-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec228d2
commit 044ff22
Showing
12 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
**Topics**: | ||
- [Introduction](Introduction.md) | ||
- | ||
- [Sorting_Algorithms](Sorting_Algorithms) |
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Bubble Sort | ||
- [Bubble Sort](#bubble-sort) | ||
- [Algorithm](#algorithm) | ||
- [Illustration](#illustration) | ||
- [Code](#code) | ||
- [Optimized Code](#optimized-code) | ||
- [Complexities](#complexities) | ||
- [Time](#time) | ||
- [Space](#space) | ||
- [Stability](#stability) | ||
|
||
## Algorithm | ||
1. The idea is to move 1st max element to right end and then 2nd max element to end and so on till the array is sorted. | ||
2. For this, we keep on comparing two adjacent elements and swap them if they are out of order (prev>current) | ||
3. Moving one element like this is called one pass | ||
4. We end doing n-1 passes in total (since first element will be naturally in order after n-1 passes). | ||
|
||
## Illustration | ||
![](Assets/2023-02-25-10-55-06.png) | ||
|
||
![](Assets/2023-02-25-10-55-19.png) | ||
|
||
## Code | ||
```python | ||
def bubbleSort(self,arr, n): | ||
|
||
for i in range(0,n-1): # for n-1 passes | ||
|
||
for j in range(0,n-1-i): # since upto i elements are already sorted no need of checking them | ||
|
||
if (arr[j]>arr[j+1]): | ||
arr[j],arr[j+1] = arr[j+1],arr[j] | ||
|
||
``` | ||
|
||
## Optimized Code | ||
- When the array becomes sorted after a pass, we can identify it and simply stop the function. For this we check if for a pass, no element is swapped. | ||
|
||
```python | ||
|
||
def bubbleSort(self,arr, n): | ||
|
||
|
||
for i in range(0,n-1): | ||
swapped = False | ||
for j in range(0,n-1-i): | ||
|
||
if (arr[j]>arr[j+1]): | ||
arr[j],arr[j+1] = arr[j+1],arr[j] | ||
swapped = True # keep track if something swapped | ||
|
||
if not swapped: # already sorted, so no need to do anything else | ||
break | ||
``` | ||
## Complexities | ||
### Time | ||
- Before optimizing, the TC is Theta(N^2), coz we definitely need N^2 iterations for every case | ||
- After optimizing, the TC is O(N^2) i.e. worst case is O(N^2), but can be O(N) sometimes | ||
### Space | ||
- Clearly O(1), its inplace sorting | ||
|
||
### Stability | ||
- The algo is stable, since we only swap when prev>current, if they are same we dont swap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Insertion Sort | ||
- [Insertion Sort](#insertion-sort) | ||
- [Algorithm](#algorithm) | ||
- [Illustration](#illustration) | ||
- [Code](#code) | ||
- [Complexity](#complexity) | ||
- [Stability](#stability) | ||
|
||
## Algorithm | ||
- Idea is to iterate thriugh each element and insert that element in to upto now sorted array so that we can extend this sorted array. | ||
- say current element is key, to insert it at the index | ||
- start from end of upto now sorted array, say from j | ||
- if arr[j]>key, it menas key should appear before, so arr[j] should go forward one space to extend the array -> arr[j+1]=arr[j] | ||
- Do this for every key from 2nd element in the given array | ||
|
||
|
||
## Illustration | ||
![](Assets/2023-02-25-12-24-01.png) | ||
|
||
## Code | ||
```python | ||
def insertionSort(self, arr, n): | ||
for i in range(1,n): | ||
key = arr[i] | ||
j = i-1 | ||
while(j>=0 and arr[j]>key): | ||
arr[j+1] = arr[j] | ||
j-=1 | ||
arr[j+1] = key | ||
``` | ||
|
||
## Complexity | ||
- Time: O(N^2) in worst case when array is reverse sorted, O(N) in best case when array is sorted | ||
- Space: O(1), inplace sorting | ||
|
||
## Stability | ||
- It is stable,sice we use arr[j]>key, if we use >= it will become unstable | ||
- Usually preferred for small arrays and hybrid algorithms use this sort when array becomes small. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Selection Sort | ||
- [Selection Sort](#selection-sort) | ||
- [Introduction](#introduction) | ||
- [Algorithm](#algorithm) | ||
- [Illustration](#illustration) | ||
- [Code](#code) | ||
- [Complexity](#complexity) | ||
- [Stability](#stability) | ||
|
||
## Introduction | ||
- Docs less memory Writer compared Quick Sort, Merge,Insertion Sort, etc. | ||
But, Cycle Sort in more optimal for memory writer. | ||
- Basic Idea for Heap Sort | ||
|
||
## Algorithm | ||
- Idea is to move first least element to left end and second least element to left end and so on.. | ||
- For this, for each index i (upto n-2) we find the minimum element index from i to n-1 and swap them | ||
|
||
## Illustration | ||
![](Assets/2023-02-25-11-45-29.png) | ||
|
||
![](Assets/2023-02-25-11-45-46.png) | ||
|
||
## Code | ||
```python | ||
|
||
def selection_sort(arr,N): | ||
|
||
for i in range(N-1): | ||
min_idx = i | ||
for j in range(i,N): | ||
if arr[j]<arr[min_idx]: | ||
min_idx = j | ||
arr[i],arr[min_idx] = arr[min_idx],arr[i] | ||
|
||
``` | ||
|
||
## Complexity | ||
- Time: Theta(N^2), in any case it takes N^2 iterations | ||
- Space: O(1), inplace sorting | ||
|
||
## Stability | ||
- It is not a stable algorithm | ||
![](Assets/2023-02-25-11-55-23.png) |