Radix sort
how it works
The idea in a few sentences and two pictures, then one row sorted step by step.
Radix sort sorts by one digit at a time: first the ones digit, then the tens, then the hundreds. Each pass is a counting sort on that one digit. It counts the numbers into ten tallies, adds them up into totals, and sends each number from the back to its place. Numbers with the same digit keep the order the pass before left them: each pass is stable. So after each pass the row is in order by the digits read so far, and after the last pass it is in order. A number with fewer digits has 0s in front: 7 is read as 007. No two numbers are ever compared.
| after | the row |
|---|---|
| the start | 503, 72, 318, 46, 211, 39, 125 |
| the ones pass | 211, 72, 503, 125, 46, 318, 39 |
| the tens pass | 503, 211, 318, 125, 39, 46, 72 |
| the hundreds pass | 39, 46, 72, 125, 211, 318, 503 |
step 2 of 80
Sort by the ones digit. Start a tally of 0 for every digit.
what it remembers to know where it is
- digit place
- 1
- number
- not yet
- digit
- not yet
Where it says not yet, nothing is held there at that moment.
step 10 of 80
The digit of 10 in this place is 0, so the tally for 0 is now 4.
what it remembers to know where it is
- digit place
- 1
- number
- 10
- digit
- 0
step 19 of 80
8 numbers have a digit of 9 or less here.
what it remembers to know where it is
- digit place
- 1
- number
- not yet
- digit
- 9
Where it says not yet, nothing is held there at that moment.
step 27 of 80
The total for 9 is 7, so 999 goes to place 7, and the total drops to 6.
what it remembers to know where it is
- digit place
- 1
- number
- 999
- digit
- 9
step 53 of 80
The total for 0 is 1, so 500 goes to place 1, and the total drops to 0.
what it remembers to know where it is
- digit place
- 10
- number
- 500
- digit
- 0
step 80 of 80
In order, without comparing two numbers: 24 numbers counted, 27 totals added and 24 numbers placed.
what it remembers to know where it is
- digit place
- not yet
- number
- not yet
- digit
- not yet
Where it says not yet, nothing is held there at that moment.
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- for each digit place, ones first, start a tally of 0 for every digitPython lines 9, 10, 11, 12, 13, 14 and 29
- count each number into the tally for its digit in that placePython lines 15, 16, 17, 18 and 19
- add each tally to the ones before itPython lines 21 and 22
- from the last number back to the first, send it to the place its digit's total says, and take one offPython lines 24, 25, 26, 27 and 28
the same in Python
The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.
def radix_sort(numbers):
"""Return a list of whole numbers from 0 up in order, smallest first.
Sort by the ones digit, then by the tens, then by the hundreds, one pass
for each digit of the biggest number. Each pass is a counting sort on
one digit: it keeps numbers with the same digit in the order the pass
before left them. No two numbers are ever compared.
"""
ordered = [0] * len(numbers) # a second row, used by every pass
place = 1 # the ones digit first
more = True # is there a digit place still to sort by?
while more:
more = False
tally = [0] * 10 # a tally of 0 for every digit
for number in numbers: # count each number under its digit
rest = number // place # cut off the digits right of this place
tally[rest % 10] += 1
if rest >= 10: # a digit further left: another pass
more = True
for digit in range(1, 10): # add the total before it to each tally
tally[digit] += tally[digit - 1]
for number in reversed(numbers): # from the last number back
digit = number // place % 10
tally[digit] -= 1 # take one off its digit's total
ordered[tally[digit]] = number # and send it to that place
numbers, ordered = ordered, numbers # the two rows swap
place *= 10 # next, the digit to the left
return numbers
how it scales
How the placings grow, from ten numbers to ten million.
Each step across or up is ten times the one before, a log-log plot. Both lines climb one step up for each step across: ten times the numbers, ten times the placings. The worst line sits three times higher, since it makes three passes and the best makes one.
placed, ten times more at each mark up
, how many numbers are in the row, ten times more at each mark to the right
- a number with three digits, the most the watch page's input box takes, so three passessolid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times n, 10 times the work
- every number below 10, so one passsolid where measured by running it; dashed where its formula carries it on to ten million
| placed | time | |
|---|---|---|
| 1,000 | 3,000 | about 3 microseconds |
| 100,000 | 300,000 | about 300 microseconds |
| 10,000,000 | 30,000,000 | about 30 milliseconds |
The placed are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.
make these counts yourself
Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.
"""Count radix sort's numbers placed yourself, as the plot on this page does.
Run it with Python 3.12 or later. For each size n it prints the numbers
placed on a row of one digit each, on a row whose biggest number has three
digits, and on a row of n different numbers. The counts are the same on any computer; only
the time taken differs.
"""
class Counted:
"""A whole number that counts every time a digit is read from it."""
reads = 0
def __init__(self, value):
self.value = value
def __floordiv__(self, place):
Counted.reads += 1
return self.value // place
def radix_sort(numbers):
"""Return a list of whole numbers from 0 up in order, smallest first.
Sort by the ones digit, then by the tens, then by the hundreds, one pass
for each digit of the biggest number. Each pass is a counting sort on
one digit: it keeps numbers with the same digit in the order the pass
before left them. No two numbers are ever compared.
"""
ordered = [0] * len(numbers) # a second row, used by every pass
place = 1 # the ones digit first
more = True # is there a digit place still to sort by?
while more:
more = False
tally = [0] * 10 # a tally of 0 for every digit
for number in numbers: # count each number under its digit
rest = number // place # cut off the digits right of this place
tally[rest % 10] += 1
if rest >= 10: # a digit further left: another pass
more = True
for digit in range(1, 10): # add the total before it to each tally
tally[digit] += tally[digit - 1]
for number in reversed(numbers): # from the last number back
digit = number // place % 10
tally[digit] -= 1 # take one off its digit's total
ordered[tally[digit]] = number # and send it to that place
numbers, ordered = ordered, numbers # the two rows swap
place *= 10 # next, the digit to the left
return numbers
def count(row):
"""How many numbers radix sort places on this row.
Each pass reads every number's digit twice, once to count it and once
to place it, so the numbers placed are half the digits read.
"""
Counted.reads = 0
radix_sort([Counted(x) for x in row])
return Counted.reads // 2
if __name__ == "__main__":
print(f"{'n':>7} {'one digit':>10} {'three digits':>13} {'all different':>14}")
for n in [10, 100, 1000, 10000, 100000]:
one_digit = [9 - i % 10 for i in range(n)] # 9, 8, ..., 0, 9, 8, ...
three_digits = [999 - i % 1000 for i in range(n)] # 999, 998, ...
different = list(range(n - 1, -1, -1)) # n - 1 down to 0
print(f"{n:>7} {count(one_digit):>10} {count(three_digits):>13} {count(different):>14}")
extra memory, the places the sort holds beyond the row itself, ten times more at each mark up
, how many numbers are in the row, ten times more at each mark to the right
- every row, whatever its ordersolid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times n, 10 times the room
best, worst and average
The order of the row never changes a count. Only two things do: how many numbers there are, and how many digits the biggest has. So the best case is one digit each, and the worst is three. There is no average column: every order of the same numbers costs the same. So the average over the orders is that same cost: 5 placings for 1, 2, 3, 4, 5 in any order. It lies between the best's and the worst's .
A case is one kind of input, the thing the method is given, such as a row back to front, whatever its length. A bound says how fast that case's cost grows as the input gets longer.
The counts are in the grid. A count is often the sum of pieces that grow at different speeds, and on long inputs the fastest-growing piece swamps the rest. For example, a count of half of n times n, less half of n: at 2,000 items, 2,000,000 less 1,000. So how a cost grows is what tells one method from another, and the bounds keep just that.
| bestevery number below 10, so one pass | worsta number with three digits, the most the watch page's input box takes, so three passes | |
|---|---|---|
| counted | ||
| totals added | 9 | 27 |
| placed | ||
| how the placed grow in each case | ||
| tight, | ||
| ceiling, | ||
| floor, | ||
extra memory: a second row of n places and 10 tallies
Extra memory is the room it needs on top of the input itself, while it runs.
| row | the biggest has | passes | placed |
|---|---|---|---|
| 1, 2, 3, 4, 5 | 1 digit | 1 | 5 |
| 5, 4, 3, 2, 1 | 1 digit | 1 | 5 |
| 50, 4, 3, 2, 1 | 2 digits | 2 | 10 |
| 500, 4, 3, 2, 1 | 3 digits | 3 | 15 |
| different numbers | the biggest is at least | fewest digits | fewest placed |
|---|---|---|---|
| 10 | 9 | 1 | 10 |
| 100 | 99 | 2 | 200 |
| 1,000 | 999 | 3 | 3,000 |
| 10,000 | 9,999 | 4 | 40,000 |
where the formula comes from
Each count above, worked out in small steps, each naming the line it comes from.
the move: price one pass
the line:The ones pass on 305, 7, 42, 618. From the back, 618 goes to place 4, 42 to place 1, 7 to place 3 and 305 to place 2. Four placings. why it is allowed: From the last number back to the first, each number is sent to its place once. The picture is the ones pass on 305, 7, 42, 618.
the move: count the passes
the line:after the row the start 305, 7, 42, 618 the ones pass 42, 305, 7, 618 the tens pass 305, 7, 618, 42 the hundreds pass 7, 42, 305, 618 305, 7, 42, 618 after each of its three passes. Each pass sends all four numbers. why it is allowed: One pass for each digit of the biggest number, ones, tens and hundreds. The biggest, 618, has three digits.
guess it, thenthe move: add the passes
the line:why it is allowed: is the placings made. By lines 1 and 2, placings in each of 3 passes.
guess it, thenthe move: gather
the line:why it is allowed: By line 3, three lots of . For 305, 7, 42, 618, that is 12.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 4, is , so it is at most .
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 5, is at most 3 times .
guess it, thenthe move: a floor
the line:why it is allowed: By line 4, is at least too.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 7, never drops below 3 times .
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 6 and 8 bound by the same growth, .
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: one digit each
the line:Radix sort on 7, 3, 9, 1, one pass on the ones digit. Four placings, and the row is 1, 3, 7, 9. why it is allowed: One pass for each digit of the biggest number, and every number is below 10, so the ones pass is the only pass. The picture sorts 7, 3, 9, 1.
the move: one placing each
the line:why it is allowed: By line 1, one pass, which sends each of the numbers once. For 7, 3, 9, 1, that is 4.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 2, is at most .
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 3, is at most 1 times .
guess it, thenthe move: a floor
the line:why it is allowed: By line 2, is at least too.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 5, never drops below 1 times .
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 4 and 6 bound by the same growth, .
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: price one pass
the line:why it is allowed: Each number adds 1 to the tally for its digit.
the move: add the passes
the line:why it is allowed: is the counts made. By line 1, in each pass, and by the first ladder's line 2, 3 passes.
guess it, thenthe move: gather
the line:why it is allowed: By line 2, three lots of . For 305, 7, 42, 618, that is 12. With one digit each, one pass counts .
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: price one pass
the line:why it is allowed: The total for digit 0 is its own tally. Each digit from 1 to 9 adds the total before it, once, since that total already holds every tally before it.
the move: add the passes
the line:why it is allowed: is the totals added. By line 1, 9 in each pass, and by the first ladder's line 2, 3 passes, however many numbers there are.
guess it, thenthe move: gather
the line:why it is allowed: By line 2, three nines. With one digit each, one pass adds 9.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: list what it keeps
the line:why it is allowed: Each pass sends the numbers into the other row, and the two rows swap for the next pass. The ten tallies are made fresh each pass. The few spare places for the digit in hand are a fixed few, left out here.
the move: add them
the line:why it is allowed: is the extra memory, by line 1. For 4 numbers, 14.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
count it yourself
Count every step on 305, 7, 42, 618. Besides the first and last rows, three rows only start a pass; the 51 left are 12 counts, 27 additions and 12 placings. Then check the placings against the formula.
| step | the row | what happens | counted | totals added | placed |
|---|---|---|---|---|---|
| 1 | 305, 7, 42, 618 | There are 4 numbers. Sort them by their last digit, then by the one before, 3 passes in all, each keeping the order the pass before left. A number short of a digit has a 0 there.305, 7, 42, 618 | 0 | 0 | 0 |
| 2 | 305, 7, 42, 618 | Sort by the ones digit. Start a tally of 0 for every digit.305, 7, 42, 618 | 0 | 0 | 0 |
| 3 | 305, 7, 42, 618 | The digit of 305 in this place is 5, so the tally for 5 is now 1.305, 7, 42, 618 | 1 | 0 | 0 |
| 4 | 305, 7, 42, 618 | The digit of 7 in this place is 7, so the tally for 7 is now 1.305, 7, 42, 618 | 2 | 0 | 0 |
| 5 | 305, 7, 42, 618 | The digit of 42 in this place is 2, so the tally for 2 is now 1.305, 7, 42, 618 | 3 | 0 | 0 |
| 6 | 305, 7, 42, 618 | The digit of 618 in this place is 8, so the tally for 8 is now 1.305, 7, 42, 618 | 4 | 0 | 0 |
| 7 | 305, 7, 42, 618 | No number has a digit of 1 or less here.305, 7, 42, 618 | 4 | 1 | 0 |
| 8 | 305, 7, 42, 618 | 1 number has a digit of 2 or less here.305, 7, 42, 618 | 4 | 2 | 0 |
| 9 | 305, 7, 42, 618 | 1 number has a digit of 3 or less here.305, 7, 42, 618 | 4 | 3 | 0 |
| 10 | 305, 7, 42, 618 | 1 number has a digit of 4 or less here.305, 7, 42, 618 | 4 | 4 | 0 |
| 11 | 305, 7, 42, 618 | 2 numbers have a digit of 5 or less here.305, 7, 42, 618 | 4 | 5 | 0 |
| 12 | 305, 7, 42, 618 | 2 numbers have a digit of 6 or less here.305, 7, 42, 618 | 4 | 6 | 0 |
| 13 | 305, 7, 42, 618 | 3 numbers have a digit of 7 or less here.305, 7, 42, 618 | 4 | 7 | 0 |
| 14 | 305, 7, 42, 618 | 4 numbers have a digit of 8 or less here.305, 7, 42, 618 | 4 | 8 | 0 |
| 15 | 305, 7, 42, 618 | 4 numbers have a digit of 9 or less here.305, 7, 42, 618 | 4 | 9 | 0 |
| 16 | 305, 7, 42, 618 | The total for 8 is 4, so 618 goes to place 4, and the total drops to 3.305, 7, 42, 618 | 4 | 9 | 1 |
| 17 | 42, 305, 7, 618 | The total for 2 is 1, so 42 goes to place 1, and the total drops to 0.42, 305, 7, 618 | 4 | 9 | 2 |
| 18 | 42, 305, 7, 618 | The total for 7 is 3, so 7 goes to place 3, and the total drops to 2.42, 305, 7, 618 | 4 | 9 | 3 |
| 19 | 42, 305, 7, 618 | The total for 5 is 2, so 305 goes to place 2, and the total drops to 1.42, 305, 7, 618 | 4 | 9 | 4 |
| 20 | 42, 305, 7, 618 | Now sort by the tens digit. Start a tally of 0 for every digit.42, 305, 7, 618 | 4 | 9 | 4 |
| 21 | 42, 305, 7, 618 | The digit of 42 in this place is 4, so the tally for 4 is now 1.42, 305, 7, 618 | 5 | 9 | 4 |
| 22 | 42, 305, 7, 618 | The digit of 305 in this place is 0, so the tally for 0 is now 1.42, 305, 7, 618 | 6 | 9 | 4 |
| 23 | 42, 305, 7, 618 | The digit of 7 in this place is 0, so the tally for 0 is now 2.42, 305, 7, 618 | 7 | 9 | 4 |
| 24 | 42, 305, 7, 618 | The digit of 618 in this place is 1, so the tally for 1 is now 1.42, 305, 7, 618 | 8 | 9 | 4 |
| 25 | 42, 305, 7, 618 | 3 numbers have a digit of 1 or less here.42, 305, 7, 618 | 8 | 10 | 4 |
| 26 | 42, 305, 7, 618 | 3 numbers have a digit of 2 or less here.42, 305, 7, 618 | 8 | 11 | 4 |
| 27 | 42, 305, 7, 618 | 3 numbers have a digit of 3 or less here.42, 305, 7, 618 | 8 | 12 | 4 |
| 28 | 42, 305, 7, 618 | 4 numbers have a digit of 4 or less here.42, 305, 7, 618 | 8 | 13 | 4 |
| 29 | 42, 305, 7, 618 | 4 numbers have a digit of 5 or less here.42, 305, 7, 618 | 8 | 14 | 4 |
| 30 | 42, 305, 7, 618 | 4 numbers have a digit of 6 or less here.42, 305, 7, 618 | 8 | 15 | 4 |
| 31 | 42, 305, 7, 618 | 4 numbers have a digit of 7 or less here.42, 305, 7, 618 | 8 | 16 | 4 |
| 32 | 42, 305, 7, 618 | 4 numbers have a digit of 8 or less here.42, 305, 7, 618 | 8 | 17 | 4 |
| 33 | 42, 305, 7, 618 | 4 numbers have a digit of 9 or less here.42, 305, 7, 618 | 8 | 18 | 4 |
| 34 | 42, 305, 618, 7 | The total for 1 is 3, so 618 goes to place 3, and the total drops to 2.42, 305, 618, 7 | 8 | 18 | 5 |
| 35 | 42, 7, 618, 305 | The total for 0 is 2, so 7 goes to place 2, and the total drops to 1.42, 7, 618, 305 | 8 | 18 | 6 |
| 36 | 305, 7, 618, 42 | The total for 0 is 1, so 305 goes to place 1, and the total drops to 0.305, 7, 618, 42 | 8 | 18 | 7 |
| 37 | 305, 7, 618, 42 | The total for 4 is 4, so 42 goes to place 4, and the total drops to 3.305, 7, 618, 42 | 8 | 18 | 8 |
| 38 | 305, 7, 618, 42 | Now sort by the hundreds digit. Start a tally of 0 for every digit.305, 7, 618, 42 | 8 | 18 | 8 |
| 39 | 305, 7, 618, 42 | The digit of 305 in this place is 3, so the tally for 3 is now 1.305, 7, 618, 42 | 9 | 18 | 8 |
| 40 | 305, 7, 618, 42 | The digit of 7 in this place is 0, so the tally for 0 is now 1.305, 7, 618, 42 | 10 | 18 | 8 |
| 41 | 305, 7, 618, 42 | The digit of 618 in this place is 6, so the tally for 6 is now 1.305, 7, 618, 42 | 11 | 18 | 8 |
| 42 | 305, 7, 618, 42 | The digit of 42 in this place is 0, so the tally for 0 is now 2.305, 7, 618, 42 | 12 | 18 | 8 |
| 43 | 305, 7, 618, 42 | 2 numbers have a digit of 1 or less here.305, 7, 618, 42 | 12 | 19 | 8 |
| 44 | 305, 7, 618, 42 | 2 numbers have a digit of 2 or less here.305, 7, 618, 42 | 12 | 20 | 8 |
| 45 | 305, 7, 618, 42 | 3 numbers have a digit of 3 or less here.305, 7, 618, 42 | 12 | 21 | 8 |
| 46 | 305, 7, 618, 42 | 3 numbers have a digit of 4 or less here.305, 7, 618, 42 | 12 | 22 | 8 |
| 47 | 305, 7, 618, 42 | 3 numbers have a digit of 5 or less here.305, 7, 618, 42 | 12 | 23 | 8 |
| 48 | 305, 7, 618, 42 | 4 numbers have a digit of 6 or less here.305, 7, 618, 42 | 12 | 24 | 8 |
| 49 | 305, 7, 618, 42 | 4 numbers have a digit of 7 or less here.305, 7, 618, 42 | 12 | 25 | 8 |
| 50 | 305, 7, 618, 42 | 4 numbers have a digit of 8 or less here.305, 7, 618, 42 | 12 | 26 | 8 |
| 51 | 305, 7, 618, 42 | 4 numbers have a digit of 9 or less here.305, 7, 618, 42 | 12 | 27 | 8 |
| 52 | 305, 42, 7, 618 | The total for 0 is 2, so 42 goes to place 2, and the total drops to 1.305, 42, 7, 618 | 12 | 27 | 9 |
| 53 | 305, 42, 7, 618 | The total for 6 is 4, so 618 goes to place 4, and the total drops to 3.305, 42, 7, 618 | 12 | 27 | 10 |
| 54 | 7, 42, 305, 618 | The total for 0 is 1, so 7 goes to place 1, and the total drops to 0.7, 42, 305, 618 | 12 | 27 | 11 |
| 55 | 7, 42, 305, 618 | The total for 3 is 3, so 305 goes to place 3, and the total drops to 2.7, 42, 305, 618 | 12 | 27 | 12 |
| 56 | 7, 42, 305, 618 | In order, without comparing two numbers: 12 numbers counted, 27 totals added and 12 numbers placed.7, 42, 305, 618 | 12 | 27 | 12 |
In all, 12 placed. The formula from section 4, , gives at : the same.
where you meet it
Radix sort is how a program sorts many short whole numbers without comparing them.
three.js, the 3D library this site loads
three.js ships a radix sort among its add-ons, one this site does not use. It works from the other end: told in digits, it groups numbers like 503, 72, 318 by the hundreds digit first, then sorts each big group by the tens. It takes whole numbers kept as 32 bits and reads 8 bits at a time instead of a digit; each bit doubles the values, so 8 bits take 256 tallies. A group of 32 numbers or fewer goes to insertion sort.
source: three.js 0.186.1, examples/jsm/utils/SortUtils.js (radixSort, radixSortBlock, insertionSortBlock), as installed in this repository
sorts that do not compare
The books list radix sort as counting sort, once per digit, for keys with a fixed number of digits. The watch page's input box takes numbers up to 999, so at most three.
source: the books' volume one, chapter 12, its table of what each sorting method needs
what is used instead
When nothing more is known about the keys than how to compare two of them, a comparing sort is used instead. The books' advice still holds: 'If you know something about your keys, use it.' (volume one, chapter 12)
how it compares
Radix sort beside the other ways to put a row in order. It compares no two numbers, so it draws no line on the plot of comparisons.
- bubble sort
- cocktail shaker sort
- insertion sort
- selection sort
- shell sort
- quick sort
- randomised quick sort
- merge sort
- heap sort
| way | best | average | worst | extra memory | keeps equal items in order | works in place | reach for it when |
|---|---|---|---|---|---|---|---|
| bubble sort | a few spare places (the walk number, the pair's place, whether the walk swapped, room for a number while two swap), the same however long the row | yes | yes | you are learning how sorting works; for real work, insertion sort, which on every order of up to 8 different numbers makes no more comparisons, counted order by order | |||
| cocktail shaker sort | a few spare places, the same however long the row | yes | yes | you are learning how sorting works; for real work, insertion sort | |||
| insertion sort | room for a few numbers (the number being placed, where its slide has reached, which number is next), the same however long the row | yes | yes | the row is short, a few dozen numbers, or nearly in order | |||
| selection sort | room for a few numbers (the place being filled, where the smallest so far stands, the number looked at, one held during a swap), the same however long the row | no | yes | moving a number costs far more than comparing two, and the row is short | |||
| shell sort | not known; between and | not worked out here; between and | a few spare places (the gap, the number picked up, its place), the same however long the row | no | yes | you want a few lines of code and no room beyond the row. On this site's shuffles of 200 to 10,000 numbers it averaged under a fifth of insertion sort's comparisons | |
| quick sort | a list of the parts still to sort, two memory places each. That is two places for each number on a row in order, and far fewer on a shuffle, about 51 for 100,000 numbers. So this plain version does not work in place, as its spare room grows with the row | no | no | you want a fast sort on shuffled rows with no second row, and equal items need not keep their order; use a version with a random or middle pivot | |||
| randomised quick sort | at most , as every row of different numbers averages that over the picks; a row with equal numbers can cost a little less | , a row of equal numbers, whatever the picks; no run costs more, since a pair meets at most once | a list of the parts waiting to be sorted, each kept as its first and last place. On average far shorter than the row, about 66 places for a million numbers. Unlucky picks can make it grow with the row | no | no | you want quick sort without a dear row in order, and equal items need not keep their order | |
| merge sort | a second row as long as the row | yes | no | you need comparisons growing no faster than on every row, equal items kept in order, or a file too big for memory sorted | |||
| heap sort | , every number the same | , shuffled; measured, no formula worked out here | , the dearest row; this page finds it only for short rows, by trying every order | a few places, however long the row | no | yes | you need a sort with no slow row and no second row, and equal items need not keep their order |
| counting sort | steps, every row alike | steps | steps, every row alike | a second row of n places and 10 tallies | yes | no | the numbers are small whole numbers, such as ratings from 0 to 9, and equal ones must keep their order |
| radix sort | placings, every number below 10 | placings, between the best and the worst, as every order costs the same | placings, three passes for numbers up to 999 | a second row of n places and 10 tallies | yes | no | many whole numbers with few digits, such as numbers from 0 to 999 |
- extra memory:
- the room it needs on top of the input itself, while it runs.
- keeps equal items in order:
- people sorted by age keep the order they arrived in within each age; in general, two items equal in what is being sorted come out in the order they went in.
- works in place:
- it rearranges the input where it stands, with only a few spare places, rather than building a copy; a different sense from a number being in place, in its final position.