Watch bubble, insertion, selection, merge, quick and heap sort race on the same row of bars with exact counters, or sort a row yourself by swapping.
Comparisons 0 Moves 0
Compares neighbours and swaps them if they are the wrong way round, again and again.
Comparisons 0 Moves 0
Takes each bar in turn and slides it left until it sits in the right place.
Comparisons 0 Moves 0
Finds the smallest bar left and swaps it to the front.
Comparisons 0 Moves 0
Splits the row in halves, sorts each half, then merges the halves by writing bars into place.
Comparisons 0 Moves 0
Picks a middle bar as a pivot, moves smaller bars before it, then sorts each side.
Comparisons 0 Moves 0
Builds a heap, then repeatedly swaps the biggest bar to the end and repairs the heap.
Swaps used 0
Fewest possible -
Your fewest at this size -
Race: press Start race and watch six algorithms sort the same row. Each tick every one does a single comparison or move, so the one with the least work finishes first. Step once shows a single step for each. Your turn: pick one bar, then another, and they swap places. The number on each bar is its height. Sort the row from shortest to tallest using as few swaps as you can. With the keyboard use the arrow keys to move along the bars and Enter or Space to pick one.
A comparison is two bars checked against each other. A move is two bars trading places (a swap), or, for merge sort, a bar written into its slot, because merge sort copies instead of swapping. The race adds the two together as work. The counters are exact: they are tallied from the steps each algorithm really takes.
In Your turn, the fewest swaps needed is the number of bars minus the number of cycles the shuffled row forms. Each swap can fix at most one cycle, so no one can do better, and the hint always finds a swap that lowers the figure by one.
As-is, no warranty. These apps are free under their listed license and run entirely in your browser. Use at your own risk — don't blame me if your PC catches fire, your dog runs away, or the math turns out wrong. Verify anything that actually matters. None of this is professional financial, medical, legal, or engineering advice.
There are two modes, chosen with the buttons at the top.
Pick how many bars (16, 32 or 48), drag the speed slider, and press Start race. Six panels each hold the same row of bars, and each algorithm sorts its own copy, one step at a time. Pause and Resume stop and start it, Step once moves every algorithm forward by exactly one step, Restart goes back to the same row, and New array shuffles a fresh one.
Every panel shows two counters. Comparisons counts the times two bars were checked against each other. Moves counts the times bars changed places: a swap of two bars, or, for merge sort, a bar written into its slot, because merge sort copies bars rather than swapping them. The race adds the two together as work. Each tick every algorithm does one step, so the algorithm with the least work finishes first. When all six are done the page lists them in order, with ties sharing a rank, and shows the counters for each.
How to read the bars: a bar being compared has a bright cap above it, a bar that just moved has diagonal stripes and a cap, a finished panel turns all its bars solid and says Done with a tick, and a waiting bar is plain. Colour is never the only clue. Each panel also has a coloured dot beside its name, and each algorithm has a one-line description underneath.
Nothing starts by itself. If your device asks for reduced motion, the picture is redrawn at most ten times a second, and Step once lets you go through it at your own pace.
A row of 8, 10 or 12 bars is shuffled, and each bar shows its height as a number. Sort it from shortest to tallest by swapping two bars at a time. Pick one bar, then another, and they trade places. Pick the same bar again to let go of it. The page shows the fewest swaps possible from the start, and counts the swaps you use.
A picked bar has a dashed border and a triangle. A bar in its right place turns green and the page says so in its label. Copy result puts a short line on your clipboard, and the page address holds the sizes and puzzle numbers.
Each algorithm is a generator that yields one step at a time and works on its own copy of the row. A step is a comparison of two positions, a swap of two positions, or a write of one value into one position. The page applies every step to the row it is drawing, and the counters are tallied from those same steps, so they cannot disagree with what you see.
| Algorithm | What its steps are |
|---|---|
| Bubble sort | compares neighbours and swaps them when out of order, stopping early when a pass makes no swap |
| Insertion sort | takes each bar and swaps it left, one place at a time, until it sits right |
| Selection sort | finds the smallest bar left and swaps it to the front: always n(n-1)/2 comparisons, at most n-1 swaps |
| Merge sort | sorts halves recursively, then merges by writing bars into place from a copy |
| Quick sort | uses the middle bar as the pivot, moves smaller bars before it, then sorts both sides |
| Heap sort | builds a heap, then repeatedly swaps the biggest bar to the end and repairs the heap |
For the puzzle, the fewest swaps of any two bars that sort a row is the number of bars minus the number of cycles in the row’s permutation. Each swap can split one cycle, so nobody can do better. The hint swaps the correct bar into the first wrong place, which splits a cycle every time, so following the hints finishes in exactly the fewest swaps.
| Claim | How it is checked |
|---|---|
| Every algorithm sorts correctly | more than 2,300 arrays each (random, repeated values, sorted, reversed, all equal, empty and single bars) compared with Array.sort |
| The steps tell the truth | replaying the steps on a fresh copy gives the sorted row, and the counters equal the number of steps of each kind |
| Counters are exact | each algorithm compared with a second, separately written version that counts comparisons and moves |
| Closed forms hold | selection sort makes n(n-1)/2 comparisons; bubble and insertion sort make as many swaps as the row has inversions; a sorted row costs bubble and insertion sort n-1 comparisons and no moves |
| Stepping in pieces gives the same result | racers advanced in random chunk sizes end where a full run ends |
| Ties rank fairly | equal work shares a rank, in the order 1, 2, 2, 4 |
| The fewest-swaps figure is exact | compared with a breadth-first search over every row of up to 7 bars (5,913 rows) |
| Hints are right | 600 puzzles solved by following hints take exactly the fewest swaps, and each hint lowers the figure by one |
| Undo is an exact round trip | replaying the recorded swaps on the start row always gives the current row |
Nothing is uploaded and there is no account. The fewest swaps you used at each row size without a hint are stored in your browser as small whole numbers. Clearing site data removes them.
Free to play, no ads, nothing uploaded. These games are original and are not affiliated with any other game or brand.