Sorting Algorithms
Explore different sorting algorithms through interactive visualizations. Compare their performance, understand their mechanics, and see how they work step-by-step.
Bubble Sort
BeginnerA simple sorting algorithm that repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.
Insertion Sort
BeginnerBuilds the sorted array one element at a time by repeatedly taking an element and inserting it into its correct position.
Selection Sort
BeginnerFinds the minimum element from the unsorted portion and places it at the beginning of the sorted portion.
Quick Sort
IntermediateA divide-and-conquer algorithm that picks a pivot element and partitions the array around it, then recursively sorts the sub-arrays.
O(n log n)O(log n)Merge Sort
IntermediateA stable divide-and-conquer algorithm that divides the array into halves, recursively sorts them, and then merges the sorted halves.
O(n log n)O(n)Heap Sort
IntermediateUses a binary heap data structure to sort elements. Builds a max-heap and repeatedly extracts the maximum element.
O(n log n)O(1)Radix Sort
AdvancedA non-comparison based algorithm that sorts numbers digit by digit, from least to most significant digit.
O(d × n)O(n + k)Counting Sort
IntermediateCounts occurrences of each value, then uses cumulative counts to place elements directly into their sorted positions.
O(n + k)O(n + k)Shell Sort
IntermediateGeneralized insertion sort that compares elements far apart, progressively reducing the gap for faster convergence.
O(n log² n)O(1)Tim Sort
AdvancedA hybrid algorithm combining insertion sort and merge sort. The default sort in Python and Java — optimized for real-world data.
Bucket Sort
IntermediateDistributes elements into buckets by value range, sorts each bucket individually, then concatenates the results.
O(n + k)O(n + k)Cocktail Shaker Sort
BeginnerA bidirectional variant of bubble sort that alternates sweeping left-to-right and right-to-left each pass.
Comb Sort
BeginnerImproves on bubble sort by comparing elements with a shrinking gap, eliminating small values stuck at the end.
Algorithm Comparison
Quick comparison of time and space complexities
| Algorithm | Best Case | Average Case | Worst Case | Space | Stable |
|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | ✅ Yes |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | ✅ Yes |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | ❌ No |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | ❌ No |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | ✅ Yes |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | ❌ No |
| Radix Sort | O(d×n) | O(d×n) | O(d×n) | O(n+k) | ✅ Yes |
| Counting Sort | O(n+k) | O(n+k) | O(n+k) | O(n+k) | ✅ Yes |
| Shell Sort | O(n log n) | O(n^1.5) | O(n²) | O(1) | ❌ No |
| Tim Sort | O(n) | O(n log n) | O(n log n) | O(n) | ✅ Yes |
| Bucket Sort | O(n+k) | O(n+k) | O(n²) | O(n+k) | ✅ Yes |
| Cocktail Shaker | O(n) | O(n²) | O(n²) | O(1) | ✅ Yes |
| Comb Sort | O(n log n) | O(n²/2^p) | O(n²) | O(1) | ❌ No |