Bubble Sort |
O(n) |
$O(n^2)$ |
$O(n^2)$ |
Best: already sorted. |
Compare neighbors, swap them if not in order |
|
|
|
|
Worst: reverse sorted. |
|
Selection Sort |
$O(n^2)$ |
$O(n^2)$ |
$O(n^2)$ |
fixed nature. |
Repeatedly select min/max element from unsorted portion and swap with first unsorted element |
Insertion sort |
O(n) |
$O(n^2)$ |
$O(n^2)$ |
Best: already sorted. |
Compare values in turn, shift to left until a smaller value is found |
|
|
|
|
Worst: reverse sorted. |
|
Merge Sort |
O(n log n) |
O(n log n) |
O(n log n) |
Always O(n log n) due to merging. |
Divide, sort recursively, merge sorted sublists. |
Quick Sort |
O(n log n) |
O(n log n) |
$O(n^2)$ |
Best: Pivot divides array evenly. |
Recursively partition based on pivot, moving smaller numbers to the left of the partition |
|
|
|
|
Average: Random pivot selection. |
|
|
|
|
|
Worst: Poor pivot selection. |
|
Heap Sort |
O(n log n) |
O(n log n) |
O(n log n) |
Always O(n log n) due to heap property. |
Remove first element from max heap, place it at the end and reduce size. Heapify and repeat. |