How sorting algorithms work
Sorting means putting things in order, here from the shortest bar to the tallest. You can glance at a row of bars and see roughly where each one goes. A computer can't. It can only look at one or two bars at a time and decide what to do next. A sorting algorithm is the plan it follows to do that.
Some plans are simple and slow, some are clever and fast, and a couple are jokes. This page goes through all 21 in Sort Sounds. Each one has a player where you can watch it sort a few numbered bars one step at a time, with every step explained.
The four moves
Every algorithm here is built from the same four moves. Each one counts as a step, and in the visualizer each step makes a beep, low for a short bar and high for a tall one.
Watching one step at a time
Each algorithm below has a player with a few numbered bars. Press Step to take one step and read what happened, or Play to watch it go. Back undoes a step, the slider jumps anywhere in the run, and New shuffle mixes the bars up differently.
- White marks under the bars show which bars the step is about.
- Green under a bar means it's in its final spot.
- A shaded patch is the part of the row the algorithm is working on.
- The tray on the right holds bars the algorithm is remembering or has copied aside.
- A dashed line marks a pivot's height, in the two quicksorts.
The whole sort in one picture
Each algorithm also gets a picture of a whole run on 64 bars. Every line across the picture is the row at one moment, with each bar drawn as a dot of its color. The top line is the shuffled start and the bottom line is the sorted end, so time runs down the picture.
This one is insertion sort. The sorted part on the left grows wider as it goes, slowly at the end because the last bars have the furthest to slide. All the pictures start from the same shuffle, so you can compare them.
The race
The same shuffled bars, the same speed, every algorithm at once. Fewer steps means an earlier finish. Click one to read about it.
Stooge sort takes a few minutes unless you turn the speed up. Bogosort sits this one out. Even on 16 bars it would need about 20 trillion tries.
Why some are so much faster
The table counts the steps each algorithm takes on 100 shuffled bars and on 1,000. Ten times as many bars doesn't mean ten times the work.
| Algorithm | 100 bars | 1,000 bars | Times the work | 1,000 bars at 1,000 steps a second |
|---|---|---|---|---|
| Counting steps | ||||
The slow and simple ones compare nearly every bar with nearly every other bar. 100 bars make about 5,000 pairs and 1,000 bars make about 500,000, so ten times the bars is about a hundred times the work.
The fast ones keep cutting the row in half. You can only halve 1,000 bars about 10 times before the pieces are single bars, so each bar only takes part in about 10 rounds. Ten times the bars is about fifteen times the work.
The counting and radix sorts never compare two bars. They go along the row a set number of times, so ten times the bars is about ten times the work, or a little more for radix sort, which needs an extra digit for the bigger numbers. The catch is that they only work on things like numbers, which can be counted or split into digits.
Shell sort and comb sort land in between. Nobody has worked out exactly how fast Shell sort is with the best gaps.
Back to the visualizer.