| Algorithm | Best | Average | Worst | Space | Stable |
|---|---|---|---|---|---|
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
| Insertion | O(n) | O(n²) | O(n²) | O(1) | Yes |
Sorting algorithms rearrange an array of elements into ascending or descending order through a sequence of comparisons and swaps. Watching these operations side by side reveals why asymptotic complexity matters as dataset size grows.
Bubble Sort and Selection Sort repeatedly scan the unsorted region in O(n²) comparisons, making them simple to implement with O(1) auxiliary memory but slow on large arrays. Insertion Sort builds a sorted prefix one item at a time and runs in O(n) best-case time on nearly sorted inputs, which is why hybrid production sorters (such as Timsort) switch to Insertion Sort for small subarrays.
Merge Sort recursively splits the array in half and merges sorted runs in guaranteed O(n log n) time while preserving equal-element order (stable), at the cost of O(n) extra space. Quick Sort partitions elements around a pivot in-place with excellent cache locality and O(n log n) average time. Heap Sort maintains a binary max-heap to guarantee O(n log n) time in O(1) auxiliary memory.
On typical random arrays, Quick Sort, Merge Sort, and Heap Sort are the fastest with O(n log n) average time complexity. On nearly sorted inputs, Insertion Sort can finish even faster in O(n) linear time.
All three are O(n²) in-place sorting algorithms, but they behave differently: Bubble Sort repeatedly swaps adjacent out-of-order pairs, Selection Sort scans for the minimum element to make at most n swaps, and Insertion Sort inserts each item into its sorted prefix (running in O(n) on nearly sorted data).
Choose Merge Sort when you need guaranteed O(n log n) performance and stability (preserving equal-element order). Choose Quick Sort for fast in-place average performance with great cache locality. Choose Heap Sort when you need guaranteed O(n log n) worst-case time with O(1) extra memory.
Yes. As each algorithm runs, the visualizer highlights active comparisons, swaps, and sorted elements in color while counting total comparisons, swaps, and elapsed time on a live leaderboard.