94Technology

How computers sort things

A computer sorts by comparing two items at a time. Sort a row by hand, watch three methods race on one list and see how the work grows with the length of the list.

The table

Putting things in order, one comparison at a time

A computer cannot look at a list and see at once that it is out of order. It can only compare two items and ask which is bigger. Sorting is the art of putting a list in order with as few of these comparisons as possible. It matters because everything from a phone book to a search result has to be sorted.

Three trays on the table. In the first, sort a row of bars yourself by swapping neighbours, and see how few swaps were possible. In the second, watch three methods sort the same list. In the third, see how the work grows when the list gets longer. The methods are run on the page and the comparisons are counted. Without a script each tray shows one picture or table.

First tray

Sort a row by hand

Here are eight bars of different heights in a jumble. You may only swap a bar with the one to its right. Press a bar to swap it with its neighbour. The aim is to put them in order, shortest on the left.

Every pair of bars that stands the wrong way round, a taller one to the left of a shorter one, is called an inversion. One swap of neighbours fixes exactly one inversion at most. So the fewest swaps that can sort a row is the number of inversions at the start, and the page tells you what it is. Beat it, if you can.

This row has 14 wrong pairs, so at least 14 swaps of neighbours are needed.

Second tray

Three methods on one list

Bubble sort walks along the list swapping any neighbours that are the wrong way round, again and again until nothing moves. Insertion sort takes each item in turn and slides it left until it meets a smaller one. Merge sort splits the list in halves, sorts each half, and then merges the two sorted halves by always taking the smaller of the two front items.

Pick a method and press Next step, or Run. The page counts the comparisons and the moves. All three give the same sorted list, but not at the same cost. Press New list to try another one.

Start
Comparisons each method makes on this list of 12
MethodComparisons
Bubble sort66
Insertion sort44
Merge sort33

This list has 35 wrong pairs. Press Next step.

Third tray

How the work grows

Bubble and insertion sort compare a lot of pairs. On a long list the number can grow like the square of its length, so ten times as many items is about a hundred times the work. Merge sort grows much more slowly, a little more than the length itself.

Slide the length of the list. The page sorts a shuffled list of that length with each method and counts the real comparisons. There is also a floor: no method that only compares pairs can always do better than the logarithm of n factorial. Merge sort comes close to it. Methods that do not compare, such as counting items, can get round the floor, but only for special kinds of data.

Bubble4,884Insertion2,481Merge537Comparisons, to the same scale
Comparisons on a shuffled list of 100 items, and the limits
MethodComparisons
Bubble sort4,884
Insertion sort2,481
Merge sort537
Most for bubble or insertion4,950
Floor for any comparing method525

On 100 shuffled items merge sort makes 537 comparisons and bubble sort 4,884. No comparing method can always do better than 525.

Key takeaways

  1. A computer sorts by comparing two items at a time. A good method uses few comparisons.
  2. A swap of neighbours fixes one wrong pair at most, so the fewest such swaps is the number of inversions.
  3. Bubble sort and insertion sort can need about n times n minus 1, over 2 comparisons. Merge sort needs about n times the base 2 logarithm of n.
  4. Ten times as many items is about a hundred times the work for bubble sort, and only a little more than ten times for merge sort.
  5. No sort that only compares pairs can always beat the base 2 logarithm of n factorial. Merge sort comes close.

Sources

Some of these sites could not be opened while this page was written, so some facts were checked through search results that quote them. Merge sort credited to John von Neumann in 1945 with a fuller account in 1948 appeared in a reference source and several others. That a swap of neighbours removes exactly one inversion, so the fewest such swaps equals the inversions, that a list of n items has at most n times n minus 1, over 2 inversions, and that the average is n times n minus 1, over 4, appeared in lecture notes. That a comparison sort needs at least log base 2 of n factorial comparisons in the worst case, which is about n log n, appeared in several places, and merge sort meets the bound closely. The counts of comparisons on the page come from running plain versions of the three methods on seeded lists and may differ a little from other versions of the same methods, for instance a bubble sort that does not stop early. Counting sort and radix sort are mentioned only as ways to avoid comparing. Nothing here is advice.