Comparison of the time complexity of various sorting algorithms
Sorting Algorithm |
Worst time complexity |
Average time complexity |
Optimal time complexity |
Space complexity |
stability |
Bubble Sort |
O (n ^ 2) |
O (n ^ 2) |
O (n) |
O (1) |
stable |
Insertion sort |
O (n ^ 2) |
O (n ^ 2) |
O (n) |
O (1) |
stable |
Hill sort |
O (n ^ 2) |
O (n ^ 1.5) |
O (nlogn) |
O (1) |
Unstable |
Select sort |
O (n ^ 2) |
O (n ^ 2) |
O (n ^ 2) |
O (1) |
Unstable |
Quick sort |
O (n ^ 2) |
O (nlogn) |
O (nlogn) |
O (logn) |
Unstable |
Merge sort |
O (nlogn) |
O (nlogn) |
O (nlogn) |
O (n) |
stable |
Heap sort |
O (nlogn) |
O (nlogn) |
O (nlogn) |
O (1) |
Unstable |
Count sort |
O(n+k) |
O(n+k) |
O(n+k) |
O(k) |
stable |
Bucket sort |
O (n ^ 2) |
O(n+k) |
O(n+k) |
O (n) |
Depends on the sorting in the bucket |
Base sort |
O(d(n+k)) |
O(d(n+k)) |
O(d(n+k)) |
O(n+k) |
stable |