Comparison based Algorithms (compare elements inside array for sorting)
Selection Sort
Insertion Sort
Bubble Sort
Quick Sort
Merge Sort
Non-Comparison based Algorithms
Bucket Sort
Radix Sort
Time Complexity Overview for Every Algorithm
- Selection Sort
Best Case: O (n^2) even if the list is already sorted, we still have to traverse every element.
Worst Case: O (n^2) every element is swapped, that results in quadratic time complexity.
- Insertion Sort
Best Case: O (n) even if the list is already sorted, it still makes n-1 comparisons without swaps.
Worst Case: O (n^2) if list is sorted in reverse order, in makes max swaps and comparisons.
- Bubble Sort
Best Case: O (n) when list is sorted and only pass is needed.
Worst Case: O (n^2) when list is sorted in reverse order, max swaps and comparisons required.
- Quick Sort
Best Case: O (nlogn) when the selected pivot divides array into two equal parts.
Worst Case: O (n^2) when selected pivot is either smallest or largest element.
- Merge Sort
Best Case: O (nlogn) continue dividing the array into halves until each half contains 1 element.
Worst Case: O (nlogn) continue dividing the array into halves until each half contains 1 element.
- Bucket Sort
Best Case: O (n+k) elements are uniformly distributed across buckets.
Worst Case: O (n^2) all elements are placed in a single bucket, quadratic sorting is used.
- Radix Sort
Best Case: O (kn) when elements have same number of digits or significant positions.
Worst Case: O (kn) each element has different number of digits or significant positions.
- Heap Sort
Best Case: O (nlogn) heap is already balanced and only heapify operations are required.
Worst Case: O (nlogn) heap is unbalanced, heapify operations are required for every element.