Table of Contents

Definition - Arrangement/Ordering of items in a sequence based upon some condition is Sorting.

Summary

AlgorithmBest Case TCWorst Case TCSpecial TCIn-placeStableSC
Bubble Sort-
Selection SortBetter 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 SortWorst case for sorted

Sorting Algorithms

  1. Bubble Sort
  2. Selection Sort
  3. Insertion Sort
  4. Merge Sort
  5. Quick Sort
  6. Heap Sort
  7. Radix Sort

Types of Sorting Algorithms

  1. Comparison Based - When an element is compared with other elements of the given data to achieve sorting.
  2. Non-Comparison Based - When sorting is achieved without any comparison between the elements.

Other classifications

  1. Iterative vs Recursive
  2. Inplace vs Not-inplace
  3. Internal (Only Main memory usage) vs External (Main + Secondary memory)
  4. Stable vs Unstable

In-place Sorting Algorithm

Auxiliary memory is the memory space required by a program. Auxiliary memory requirement -

  1. At most (excluding recursion stack)
  2. 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-

  1. The number of comparisons.
  2. 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 ComparisonsNo. 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 ComparisonsNo. of SwapsComplexity
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 ComparisonsNo. of SwapsComplexity
Best Case
(Ascending Order)
Worst Case
(Descending Order)

Properties

  1. Is In-place, .
  2. Is Stable.
  3. No. of swaps = No. of inversions.
  4. Requires no swaps in best case scenario and swaps in worst case scenario.
  5. Guarantee - After some pass the largest elements are at their correct positions at the end of the array.
  6. The maximum number of passes required - .

Selection Sort

  1. In the pass, find the index with the smallest element.
  2. Swap the element at the index with the smallest element.
  3. 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 ComparisonsNo. of SwapsComplexity
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

  1. Is Unstable.
  2. Is In-place, ..
  3. Always takes passes.
  4. Each pass does exactly swap.

Insertion Sort

  1. Pick elements from left to right one by one as key elements.
  2. If the key element is less than the element at the key-1 index, swap them.
  3. Keep swapping until there are no more elements to the left of key or the element at key-1 is greater than key element.

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 ComparisonsNo. of SwapsComplexity
Best Case
(Ascending Order)
Worst Case
(Descending Order)

Properties

  1. Is In-place, .
  2. Is Stable.
  3. Always takes passes.
  4. No. of swaps = No. of inversions.
  5. 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.

  1. 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.
  2. Collect the numbers by reading the bins from key 0 to 9, preserving order.
  3. Repeat the above steps for the tens digit, then the hundreds digit, and so on.
  4. Continue for k passes, where k is the number of digits in the maximum element.
  5. The final array obtained is sorted.

Properties

  1. Not In-place, requires external data structure with .
  2. Is Stable.
  3. . 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 .