Winner
Operations
Finished

  • Compared
  • Marked (smallest, pivot or key)
  • Swapped or moved
  • In place
Place Sort Comparisons or reads Swaps or moves Operations

More about sorting algorithms

Sort match

Which sorting algorithm is fastest? It depends on the list. Sort match runs all eight sorting algorithms on the same list at once, one lane each, and counts every comparison, read, swap and move as one operation. The lanes move on as their counts go up, so you can watch the efficient sorts finish while the slow ones are still swapping, and see the order change when the list is nearly sorted or reversed. Press Play to watch all eight sorting algorithms race on the same list, or Step and Back to go one operation at a time. Drag the slider to jump anywhere. Every comparison, read, swap or move counts as one operation, and each lane moves on as its own count goes up, so the sort that needs the fewest operations finishes first. Choose how many bars and how they start, or type your own numbers. With the board selected, the arrow keys step, the space bar plays and pauses, and Home and End jump to the start and the end.

How it works

  1. Choose how many bars and how they start: random, nearly sorted, reversed, or with few unique values. Or type your own numbers.
  2. Press Play and watch the eight lanes. Step and Back move one operation at a time; the slider jumps anywhere in the race.
  3. Compare the results table, then try a nearly sorted list and a reversed one to see the ranking change.

How the race works

All eight sorts get the same list. Every comparison of two values, every digit or value that radix and counting sort read, and every swap or move counts as one operation. At each moment of the race, each lane shows how far its sort has got with that many operations. A sort that needs fewer operations reaches the end sooner. The results table lists the comparisons, the swaps or moves, and the total for every sort.

What the results show

On 40 random values, counting sort needs about 120 operations, radix sort about 240, quicksort about 260 and merge sort about 380. Heap sort follows with about 490, while selection and insertion sort need about 820 and bubble sort over 1,100. On a nearly sorted list insertion sort wins with about 50, and bubble sort comes second. On a reversed list bubble and insertion sort both need 1,560. Counting and radix sort win so often because these values are small whole numbers; on other data they cannot be used at all.

Frequently asked questions

Which sorting algorithm is the fastest?

It depends on the data. For small whole numbers, counting and radix sort need the fewest operations. For general data, quicksort and merge sort are fastest on random lists, and insertion sort is fastest on lists that are nearly sorted. Bubble and selection sort are slow on all but the smallest lists.

What counts as an operation?

Every comparison of two values, every digit or value read by radix and counting sort, and every swap or move of a value. It is a simple model: real running times also depend on memory access and the cost of each operation.

Why do counting and radix sort win so often?

Because they never compare values. They use the fact that the values are small whole numbers, so their work grows with n plus the range of the values, not with n log n. On data that is not made of small integers they do not apply.

Why does insertion sort win on a nearly sorted list?

Because its work is the number of values plus the number of pairs out of order. A nearly sorted list has very few such pairs, so each value only moves a place or two.

Are my numbers sent anywhere?

No. The race runs in your browser, and your last settings stay in this browser's local storage.

Ready to try it?

Open PictorX in your browser — no install, no credit card required to start.

Go to Editor