Document Status: In Progress
This guide is intended to cover a summary of major sorting algorithms along with the use case and relevant details.
- Selection Sort
- Insertion Sort
- Shell Sort
- Merge Sort
- Quick Sort
- Heap Sort
- Cyclic Sort
- Bucket Sort
- Radix Sort
1. Selection Sort
Algorithm
- Select an element , compare it with rest of the elements.
- Once minimum element found, swap it
- The elements to the left will always be sorted
Psuedo Code
n -> array length
for(i = 0; i < n - 1; i++)
min = arr[i];
for(int j = i + 1 ; j < n ; j++)
if( arr[j] < min)
min_index = j
min = arr[j]
//Swap the element with min index
swap(arr, i, min_index)
Example

Compares
In selection sort, every element is compared with all the elements. It taks around T(~n 2/2) exchanges
Swaps/Exchange
In worst case, there would be T(n) swaps
Cons
- For sorted array, Selection sort takes O(n2)
- Input length doesnt affect the running time of the algorithm
Properties
LC Questions
TBD
2. Insertion Sort
Algorithm
- Take an element, and keep on insert it into its proper place in already sorted array
- The elements to the left will always be sorted
Pseudocode
for(int i = 0; i < n; i++)
for(int j = i; j >0 && arr[j] < arr[j-1];j--)
swap(arr, j, j-1)
Example

Compares
Average: It taks around T(~n 2/4) compares
Best: N - 1 Comares
Worst:T(~n 2/2)
Swaps/Exchange
In average case, there would be T(~n 2/4) swaps
In worst case, there would be T(~n 2/2) swaps
In best case, there would be 0 swaps
Property
- No swaps when array is already sorted
- Works best for partially sorted array
LC
3. Shell Sort
Algorithm
- It is improvement on Insertion Sort
- We divide the array into mini arrays for kind of apply insertion sort on smaller arrays
TBD...
Resources:
- Algorithms - Fourth Edition , By Sedwig