How to use it
Pick an algorithm and press Play. Each bar is one number in an array, and its height is its value. The algorithm rearranges the bars until they rise from left to right. The animation advances one basic operation at a time: a comparison of two elements, a swap, or (in merge sort) a copy or a write. Pause whenever you like and use Step to move forward exactly one operation while the pseudocode panel highlights the line being executed.
Reset puts the original array back, and changing the algorithm keeps the same array, so you can replay one input with every method. Tick “Compare with” to run two algorithms side by side on identical input. They take turns, one operation each, and each has its own counters.
Controls
- Play / Pause / Resume: starts and stops the animation. On a keyboard, Space does the same and Esc pauses.
- Step: one operation forward (key S). Reset: back to the starting array with all counters at zero (key R).
- Speed: from 1 to 2,000 operations per second.
- Array size: 5 to 200 bars. Input type: random, nearly sorted, reversed, or few unique values (at most five different heights).
- New array: draws a fresh array of the chosen type (key N).
How it works
All seven are comparison sorts: they learn about the data only by asking whether one element is larger than another. The page first runs the chosen algorithm on a copy of the array and records every operation, then replays that list. The counters follow fixed rules. A comparison reads two elements, so it adds 2 array accesses. A swap is two reads and two writes, so it adds 4; swapping an element with itself is skipped. Merge sort does not swap. It copies a range into a buffer (one step, the whole range lights up) and writes the elements back in order, so its second counter is labelled Writes, and every copied or written element adds 2 accesses (one read, one write).
Textbooks describe several variants of each algorithm, so here is exactly what runs. Bubble sort stops after a pass with no swaps. Insertion sort moves each element left by swapping neighbours. Shell sort uses Knuth’s gaps 1, 4, 13, 40, 121 (each is three times the previous plus one). Merge sort is the top-down version with a buffer. Quick sort uses the Lomuto partition with the last element as the pivot. Heap sort builds a max-heap from the bottom up, then repeatedly moves the largest element to the end.
| Algorithm | Best | Average | Worst | Extra space | Stable |
|---|---|---|---|---|---|
| Bubble | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Selection | O(n²) | O(n²) | O(n²) | O(1) | No |
| Insertion | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Shell | O(n log n) | unknown | O(n1.5) | O(1) | No |
| Merge | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
| Heap | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
Three entries need a note. No exact average is known for Shell sort with these gaps; it lies between n log n and n1.5. Quick sort’s extra space is its recursion stack, which is O(log n) on average but O(n) in the worst case. Heap sort’s best case assumes distinct values; if all values are equal it finishes in O(n). A sort is stable when elements with equal values keep their original order.
Things to try
- Run insertion sort on a nearly sorted array, then on a reversed one. An already sorted array needs only n − 1 comparisons; a reversed one needs n(n − 1)/2.
- Give quick sort a reversed array. The pivot is always the smallest or largest value left, so the recursion depth climbs to n − 1 and the comparisons reach n(n − 1)/2. Then try random input.
- Watch selection sort’s comparison counter. It is n(n − 1)/2 for every input (780 for 40 bars), yet the algorithm never needs more than n − 1 swaps.
- Compare bubble sort with insertion sort. Their swap counts always match, because each swap of neighbours removes exactly one out-of-order pair.
- Set 200 bars and compare merge sort with heap sort. Both are O(n log n), but they need different numbers of comparisons.
Frequently asked questions
Which sorting algorithm is the fastest?
There is no single winner. On large random arrays the O(n log n) methods (merge, quick and heap sort) leave the quadratic ones far behind. On tiny or nearly sorted arrays insertion sort is hard to beat, which is why sorting routines in real programming languages are usually hybrids of two or three of these ideas.
Why is quick sort slow on sorted or reversed input here?
Because the pivot is always the last element. On ordered input that is the largest or smallest value, so each partition removes just one element. Production versions choose a random or median-of-three pivot, which makes this worst case very unlikely.
Why does stability matter?
It matters when you sort records twice, for example by name and then by age. With a stable sort, people of the same age stay in name order. Here bubble, insertion and merge sort are stable.
Do the counters show which algorithm is fastest on a real computer?
Only roughly. They count operations exactly, but real speed also depends on memory caches, branch prediction and how expensive one comparison is. Quick sort often wins in practice even when merge sort makes fewer comparisons.