Table of Contents
Definition - Arrangement/Ordering of items in a sequence based upon some condition is Sorting.
Summary
| Algorithm | Best Case TC | Worst Case TC | Special TC | In-place | Stable | SC |
|---|---|---|---|---|---|---|
| Bubble Sort | - | ✅ | ✅ | |||
| Selection Sort | Better worst case no. of swaps than Bubble Sort | ✅ | ❌ | |||
| Insertion Sort | if pre-sorted | ✅ | ✅ | |||
Radix Sort | - | - | input-independent | ❌ | ✅ | |
Merge Sort | - | - | input-independent | ❌ | ✅ | Aux mem + Recur. stack |
| Quick Sort | Worst case for sorted | ❌ | ❌ |
Sorting Algorithms
- Bubble Sort
- Selection Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- Heap Sort
- Radix Sort
Types of Sorting Algorithms
- Comparison Based - When an element is compared with other elements of the given data to achieve sorting.
- Non-Comparison Based - When sorting is achieved without any comparison between the elements.
Other classifications
- Iterative vs Recursive
- Inplace vs Not-inplace
- Internal (Only Main memory usage) vs External (Main + Secondary memory)
- Stable vs Unstable
In-place Sorting Algorithm
Auxiliary memory is the memory space required by a program. Auxiliary memory requirement -
- At most (excluding recursion stack)
- Recursion stack at most
Stable Sorting Algorithm
If (not necessarily equal, just have the same priority of ordering) and in the given input occurs before , then if in the sorting output too occurs before we say that the algorithm in stable.
Inversion in an Array
If but , then is an inversion pair.
The time complexity of any Comparison based Sorting Algorithm depends upon two things-
- The number of comparisons.
- The number of inversions (Number of swaps).
Bubble Sort
Bubble sort works in “passes”. If checks for each index whether the current element and the element after are forming an inversion pair. If an inversion pair is formed then we swap the elements, else we skip.

Algo1
1. BubbleSort1(A,n) {
2. for (pass=1; pass<=n-1; pass++) {
3. for (j=1; j<=n-1; j++) {
4. if (A[j] > A[j+1]) {
5. swap(A[j], A[j+1])
6. }
7. }
8. }
9. }| No. of Comparisons | No. of Swaps | |
|---|---|---|
| Best Case (Ascending Order) | ||
| Worst Case (Descending Order) |
Algo2
As any pass would already fix large elements at their correct positions, we don’t need to run each pass times.
1. BubbleSort1(A,n) {
2. for (pass=1; pass<=n-1; pass++) {
3. for (j=1; j<=n-pass; j++) {
4. if (A[j] > A[j+1]) {
5. swap(A[j], A[j+1])
6. }
7. }
8. }
9. }| No. of Comparisons | No. of Swaps | Complexity | |
|---|---|---|---|
| Best Case (Ascending Order) | |||
| Worst Case (Descending Order) | |||
| Overall |
Algo3 (default)
When number of swaps in the current arrangement is , we can say that the array has been sorted and avoid unnecessary further passes.
BubbleSort1(A,n) {
for (pass=1; pass<=n-1; pass++) {
flag=0
for (j=1; j<=n-pass; j++) {
if (A[j] > A[j+1]) {
swap(A[j], A[j+1])
flag=1
}
}
if (flag==1) {break}
}
}| No. of Comparisons | No. of Swaps | Complexity | |
|---|---|---|---|
| Best Case (Ascending Order) | |||
| Worst Case (Descending Order) |
Properties
- Is In-place, .
- Is Stable.
- No. of swaps = No. of inversions.
- Requires no swaps in best case scenario and swaps in worst case scenario.
- Guarantee - After some pass the largest elements are at their correct positions at the end of the array.
- The maximum number of passes required - .
Selection Sort
- In the pass, find the index with the smallest element.
- Swap the element at the index with the smallest element.
- Repeat until array is sorted

Pseudocode and TC
SelectionSort(A, n) {
for (pass=1; pass<=n-1; pass++) {
min=pass
for (j=pass+1; j<=n; j++) {
if A[j] < A[min] {
min=j
}
}
if min != pass {
swap(A[pass], A[min])
}
}
}| No. of Comparisons | No. of Swaps | Complexity | |
|---|---|---|---|
| Best Case (Ascending Order) | |||
| Worst Case (Descending Order) | |||
| Overall | |||
| Even though the overall best case time complexity is worse than Bubble sort, even under worst case the algorithm only takes time complexity for the swaps. |
Properties
- Is Unstable.
- Is In-place, ..
- Always takes passes.
- Each pass does exactly swap.
Insertion Sort
- Pick elements from left to right one by one as
keyelements. - If the
keyelement is less than the element at thekey-1index, swap them. - Keep swapping until there are no more elements to the left of
keyor the element atkey-1is greater thankeyelement.

Pseudocode and TC
InsertionSort(A, n) {
for (i=2; i<=n; i++) {
key=A[i]
j=i-1
while (j > 0 and A[j]>key) {
A[j+1]=A[j]
j--
}
if (i != j+1) {
A[j+1]=key
}
}
}| No. of Comparisons | No. of Swaps | Complexity | |
|---|---|---|---|
| Best Case (Ascending Order) | |||
| Worst Case (Descending Order) |
Properties
- Is In-place, .
- Is Stable.
- Always takes passes.
- No. of swaps = No. of inversions.
- If the input list is already pre-sorted, then it takes time of where - = no. of elements = no. of inversions Check Question 2 for this.
Radix Sort
Radix means the base of the number system.
- Bin the numbers based on their unit’s digit using a key-value structure with keys
0–9, inserting elements for each key in FIFO order. - Collect the numbers by reading the bins from key
0to9, preserving order. - Repeat the above steps for the tens digit, then the hundreds digit, and so on.
- Continue for k passes, where
kis the number of digits in the maximum element. - The final array obtained is sorted.

Properties
- Not In-place, requires external data structure with .
- Is Stable.
- . base of given input. no. of digits in the maximum element of the array. no. of elements in the array.
Questions
Q1)

Sol - The time complexity of Radix Sort is where is the number of digits in the maximum element of the array.
The maximum element of the array is and the number of digits in it are . So as is some constant which wouldn’t affect the time complexity of the algorithm.
Q2)

Sol - The time complexity of Insertion sort for pre-sorted inputs is where is the number of inversions. Here the permutations can have at most inversions so the time complexity is .