Skip to content

Radix sort

ordering

Level 3 of 3

Also called Least significant digit 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.

afterthe row
the start503, 72, 318, 46, 211, 39, 125
the ones pass211, 72, 503, 125, 46, 318, 39
the tens pass503, 211, 318, 125, 39, 46, 72
the hundreds pass39, 46, 72, 125, 211, 318, 503
Radix sort on 503, 72, 318, 46, 211, 39, 125. After the tens pass the last two digits read 03, 11, 18, 25, 39, 46, 72, in order. 39, 46 and 72 share the hundreds digit 0 and keep that order.
counted: how many with each digit1422305374618digittally02112030415060708090added up: how many with that digit or smallerdigittallytotal022113203303414504604704804904sent from the last number back142230537461812346183723051424
The tens pass on 305, 7, 42, 618, which the ones pass left as 42, 305, 7, 618. Tens digit 0 has total 2, so places 1 and 2 are for 305 and 7. From the back, 7 takes place 2, then 305 place 1, the order the ones pass made.
Zeros and nines: 6 moments of its 80 steps, the steps between them left for the watch page
  1. step 2 of 80

    9995001019950101001234567890000000000

    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.

    watch from here

  2. step 10 of 80

    9995001019950101001234567894200000002

    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

    watch from here

  3. step 19 of 80

    9995001019950101001234567894666666668

    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.

    watch from here

  4. step 27 of 80

    5005001010119999901234567890466666666

    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

    watch from here

  5. step 53 of 80

    5000101110509999901234567890455556666

    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

    watch from here

  6. step 80 of 80

    0110509910150099901234567890566667777

    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.

    watch from here

the steps, in plain words
  1. for each digit place, ones first, start a tally of 0 for every digitPython lines 9, 10, 11, 12, 13, 14 and 29
  2. count each number into the tally for its digit in that placePython lines 15, 16, 17, 18 and 19
  3. add each tally to the ones before itPython lines 21 and 22
  4. 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

slope 1102104106108101102103104105106107slope 1101102103104105106107108101102103104105106107slope 1101102103104105106107108101102103104105106107

nn, 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
what a dearest row costs, at a billion placed a second
nnplacedtime
1,0003,000about 3 microseconds
100,000300,000about 300 microseconds
10,000,00030,000,000about 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

slope 1102104106108101102103104105106107slope 1101102103104105106107108101102103104105106107slope 1101102103104105106107108101102103104105106107

nn, 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 nn and the worst's 3n3n.

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.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
bestevery number below 10, so one passworsta number with three digits, the most the watch page's input box takes, so three passes
countednn3n3n
totals added927
placednn3n3n
how the placed grow in each case
tight, Θ\ThetaΘ(n)\Theta(n)Θ(n)\Theta(n)
ceiling, OOO(n)O(n)O(n)O(n)
floor, Ω\OmegaΩ(n)\Omega(n)Ω(n)\Omega(n)

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.

rowthe biggest haspassesplaced
1, 2, 3, 4, 51 digit15
5, 4, 3, 2, 11 digit15
50, 4, 3, 2, 12 digits210
500, 4, 3, 2, 13 digits315
Four rows of 5 numbers. In order or back to front, the cost is the same. The biggest number's digits set the passes, one pass for each.
different numbersthe biggest is at leastfewest digitsfewest placed
109110
100992200
1,00099933,000
10,0009,999440,000
Different whole numbers from 0 up. The smallest they can be is 0, 1, 2 and on, so the biggest is at least one less than how many. Numbered that way, ten times the numbers take more than ten times the placings.

where the formula comes from

Each count above, worked out in small steps, each naming the line it comes from.

Why does radix sort make exactly 3n3n placings when the biggest has three digits?
  1. the move: price one pass

    the line:
    one pass sendseach number once\begin{gathered} \text{one pass sends} \\ \text{each number once} \end{gathered}
    sent from the last number back130527342461812346184421733052
    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.

  2. the move: count the passes

    the line:
    three digits,so three passes\begin{gathered} \text{three digits,} \\ \text{so three passes} \end{gathered}
    afterthe row
    the start305, 7, 42, 618
    the ones pass42, 305, 7, 618
    the tens pass305, 7, 618, 42
    the hundreds pass7, 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.

  3. the move: add the passes

    the line:
    P=n+n+nP = n + n + n

    why it is allowed: PP is the placings made. By lines 1 and 2, nn placings in each of 3 passes.

  4. the move: gather

    the line:
    P=3nP = 3n

    why it is allowed: By line 3, three lots of nn. For 305, 7, 42, 618, that is 12.

  5. the move: a ceiling

    the line:
    P≤3nP \le 3n

    why it is allowed: By line 4, PP is 3n3n, so it is at most 3n3n.

  6. the move: read it as a bound

    the line:
    P∈O(n)P \in O(n)

    why it is allowed: By line 5, PP is at most 3 times nn.

  7. the move: a floor

    the line:
    P≥3nP \ge 3n

    why it is allowed: By line 4, PP is at least 3n3n too.

  8. the move: read it as a bound

    the line:
    P∈Ω(n)P \in \Omega(n)

    why it is allowed: By line 7, PP never drops below 3 times nn.

  9. the move: ceiling and floor together

    the line:
    P∈Θ(n)P \in \Theta(n)

    why it is allowed: Lines 6 and 8 bound PP by the same growth, nn.

try it with a small nn: at n=4n = 4, line 3 gives P=12P = 12, which you can work out by hand

Why does radix sort make exactly nn placings when every number is below 10?
  1. the move: one digit each

    the line:
    one digit each,so one pass\begin{gathered} \text{one digit each,} \\ \text{so one pass} \end{gathered}
    counted: how many with each digit17233941digittally00112031405060718091added up: how many with that digit or smallerdigittallytotal000111201312402502602713803914sent from the last number back17233941123411943273
    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.

  2. the move: one placing each

    the line:
    P=nP = n

    why it is allowed: By line 1, one pass, which sends each of the nn numbers once. For 7, 3, 9, 1, that is 4.

  3. the move: a ceiling

    the line:
    P≤nP \le n

    why it is allowed: By line 2, PP is at most nn.

  4. the move: read it as a bound

    the line:
    P∈O(n)P \in O(n)

    why it is allowed: By line 3, PP is at most 1 times nn.

  5. the move: a floor

    the line:
    P≥nP \ge n

    why it is allowed: By line 2, PP is at least nn too.

  6. the move: read it as a bound

    the line:
    P∈Ω(n)P \in \Omega(n)

    why it is allowed: By line 5, PP never drops below 1 times nn.

  7. the move: ceiling and floor together

    the line:
    P∈Θ(n)P \in \Theta(n)

    why it is allowed: Lines 4 and 6 bound PP by the same growth, nn.

try it with a small nn: at n=4n = 4, line 2 gives P=4P = 4, which you can work out by hand

Why does radix sort make exactly 3n3n counts when the biggest has three digits?
  1. the move: price one pass

    the line:
    one pass countseach number once\begin{gathered} \text{one pass counts} \\ \text{each number once} \end{gathered}

    why it is allowed: Each number adds 1 to the tally for its digit.

  2. the move: add the passes

    the line:
    C=n+n+nC = n + n + n

    why it is allowed: CC is the counts made. By line 1, nn in each pass, and by the first ladder's line 2, 3 passes.

  3. the move: gather

    the line:
    C=3nC = 3n

    why it is allowed: By line 2, three lots of nn. For 305, 7, 42, 618, that is 12. With one digit each, one pass counts nn.

try it with a small nn: at n=4n = 4, line 2 gives C=12C = 12, which you can work out by hand

Why does radix sort add exactly 27 totals when the biggest has three digits?
  1. the move: price one pass

    the line:
    one pass adds9 totals\begin{gathered} \text{one pass adds} \\ \text{9 totals} \end{gathered}

    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.

  2. the move: add the passes

    the line:
    A=9+9+9A = 9 + 9 + 9

    why it is allowed: AA 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.

  3. the move: gather

    the line:
    A=27A = 27

    why it is allowed: By line 2, three nines. With one digit each, one pass adds 9.

try it with a small nn: at n=4n = 4, line 2 gives A=27A = 27, which you can work out by hand

Why does radix sort need n+10n + 10 places of extra memory?
  1. the move: list what it keeps

    the line:
    a second row of n places,and a tally for each digitfrom 0 to 9\begin{gathered} \text{a second row of } n \text{ places,} \\ \text{and a tally for each digit} \\ \text{from 0 to 9} \end{gathered}

    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.

  2. the move: add them

    the line:
    M=n+10M = n + 10

    why it is allowed: MM is the extra memory, by line 1. For 4 numbers, 14.

try it with a small nn: at n=4n = 4, line 2 gives M=14M = 14, 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.

305, 7, 42, 618, every step written down
stepthe rowwhat happenscountedtotals addedplaced
1305, 7, 42, 618There 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, 618000
2305, 7, 42, 618Sort by the ones digit. Start a tally of 0 for every digit.305, 7, 42, 618000
3305, 7, 42, 618The digit of 305 in this place is 5, so the tally for 5 is now 1.305, 7, 42, 618100
4305, 7, 42, 618The digit of 7 in this place is 7, so the tally for 7 is now 1.305, 7, 42, 618200
5305, 7, 42, 618The digit of 42 in this place is 2, so the tally for 2 is now 1.305, 7, 42, 618300
6305, 7, 42, 618The digit of 618 in this place is 8, so the tally for 8 is now 1.305, 7, 42, 618400
7305, 7, 42, 618No number has a digit of 1 or less here.305, 7, 42, 618410
8305, 7, 42, 6181 number has a digit of 2 or less here.305, 7, 42, 618420
9305, 7, 42, 6181 number has a digit of 3 or less here.305, 7, 42, 618430
10305, 7, 42, 6181 number has a digit of 4 or less here.305, 7, 42, 618440
11305, 7, 42, 6182 numbers have a digit of 5 or less here.305, 7, 42, 618450
12305, 7, 42, 6182 numbers have a digit of 6 or less here.305, 7, 42, 618460
13305, 7, 42, 6183 numbers have a digit of 7 or less here.305, 7, 42, 618470
14305, 7, 42, 6184 numbers have a digit of 8 or less here.305, 7, 42, 618480
15305, 7, 42, 6184 numbers have a digit of 9 or less here.305, 7, 42, 618490
16305, 7, 42, 618The total for 8 is 4, so 618 goes to place 4, and the total drops to 3.305, 7, 42, 618491
1742, 305, 7, 618The total for 2 is 1, so 42 goes to place 1, and the total drops to 0.42, 305, 7, 618492
1842, 305, 7, 618The total for 7 is 3, so 7 goes to place 3, and the total drops to 2.42, 305, 7, 618493
1942, 305, 7, 618The total for 5 is 2, so 305 goes to place 2, and the total drops to 1.42, 305, 7, 618494
2042, 305, 7, 618Now sort by the tens digit. Start a tally of 0 for every digit.42, 305, 7, 618494
2142, 305, 7, 618The digit of 42 in this place is 4, so the tally for 4 is now 1.42, 305, 7, 618594
2242, 305, 7, 618The digit of 305 in this place is 0, so the tally for 0 is now 1.42, 305, 7, 618694
2342, 305, 7, 618The digit of 7 in this place is 0, so the tally for 0 is now 2.42, 305, 7, 618794
2442, 305, 7, 618The digit of 618 in this place is 1, so the tally for 1 is now 1.42, 305, 7, 618894
2542, 305, 7, 6183 numbers have a digit of 1 or less here.42, 305, 7, 6188104
2642, 305, 7, 6183 numbers have a digit of 2 or less here.42, 305, 7, 6188114
2742, 305, 7, 6183 numbers have a digit of 3 or less here.42, 305, 7, 6188124
2842, 305, 7, 6184 numbers have a digit of 4 or less here.42, 305, 7, 6188134
2942, 305, 7, 6184 numbers have a digit of 5 or less here.42, 305, 7, 6188144
3042, 305, 7, 6184 numbers have a digit of 6 or less here.42, 305, 7, 6188154
3142, 305, 7, 6184 numbers have a digit of 7 or less here.42, 305, 7, 6188164
3242, 305, 7, 6184 numbers have a digit of 8 or less here.42, 305, 7, 6188174
3342, 305, 7, 6184 numbers have a digit of 9 or less here.42, 305, 7, 6188184
3442, 305, 618, 7The total for 1 is 3, so 618 goes to place 3, and the total drops to 2.42, 305, 618, 78185
3542, 7, 618, 305The total for 0 is 2, so 7 goes to place 2, and the total drops to 1.42, 7, 618, 3058186
36305, 7, 618, 42The total for 0 is 1, so 305 goes to place 1, and the total drops to 0.305, 7, 618, 428187
37305, 7, 618, 42The total for 4 is 4, so 42 goes to place 4, and the total drops to 3.305, 7, 618, 428188
38305, 7, 618, 42Now sort by the hundreds digit. Start a tally of 0 for every digit.305, 7, 618, 428188
39305, 7, 618, 42The digit of 305 in this place is 3, so the tally for 3 is now 1.305, 7, 618, 429188
40305, 7, 618, 42The digit of 7 in this place is 0, so the tally for 0 is now 1.305, 7, 618, 4210188
41305, 7, 618, 42The digit of 618 in this place is 6, so the tally for 6 is now 1.305, 7, 618, 4211188
42305, 7, 618, 42The digit of 42 in this place is 0, so the tally for 0 is now 2.305, 7, 618, 4212188
43305, 7, 618, 422 numbers have a digit of 1 or less here.305, 7, 618, 4212198
44305, 7, 618, 422 numbers have a digit of 2 or less here.305, 7, 618, 4212208
45305, 7, 618, 423 numbers have a digit of 3 or less here.305, 7, 618, 4212218
46305, 7, 618, 423 numbers have a digit of 4 or less here.305, 7, 618, 4212228
47305, 7, 618, 423 numbers have a digit of 5 or less here.305, 7, 618, 4212238
48305, 7, 618, 424 numbers have a digit of 6 or less here.305, 7, 618, 4212248
49305, 7, 618, 424 numbers have a digit of 7 or less here.305, 7, 618, 4212258
50305, 7, 618, 424 numbers have a digit of 8 or less here.305, 7, 618, 4212268
51305, 7, 618, 424 numbers have a digit of 9 or less here.305, 7, 618, 4212278
52305, 42, 7, 618The total for 0 is 2, so 42 goes to place 2, and the total drops to 1.305, 42, 7, 61812279
53305, 42, 7, 618The total for 6 is 4, so 618 goes to place 4, and the total drops to 3.305, 42, 7, 618122710
547, 42, 305, 618The total for 0 is 1, so 7 goes to place 1, and the total drops to 0.7, 42, 305, 618122711
557, 42, 305, 618The total for 3 is 3, so 305 goes to place 3, and the total drops to 2.7, 42, 305, 618122712
567, 42, 305, 618In order, without comparing two numbers: 12 numbers counted, 27 totals added and 12 numbers placed.7, 42, 305, 618122712

In all, 12 placed. The formula from section 4, 3n3n, gives P=12P = 12 at n=4n = 4: 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.

comparisons on a shuffled row, on the log-log scales of section 2, one line for each way whose page is written
102104106108101010121014101102103104105106107bubble sortcocktail shaker sortinsertion sortselection sortshell sortquick sortrandomised quick sortmerge sortheap sort10110210310410510610710810910101011101210131014101102103104105106107bubble sortcocktail shaker sortinsertion sortselection sortshell sortquick sortrandomised quick sortmerge sortheap sort
  • bubble sort
  • cocktail shaker sort
  • insertion sort
  • selection sort
  • shell sort
  • quick sort
  • randomised quick sort
  • merge sort
  • heap sort
put a row in order: each way side by side
waybestaverageworstextra memorykeeps equal items in orderworks in placereach for it when
bubble sortΘ(n)\Theta(n)Θ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)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 rowyesyesyou 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Θ(n)\Theta(n)Θ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)a few spare places, the same however long the rowyesyesyou are learning how sorting works; for real work, insertion sort
insertion sortΘ(n)\Theta(n)Θ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)room for a few numbers (the number being placed, where its slide has reached, which number is next), the same however long the rowyesyesthe row is short, a few dozen numbers, or nearly in order
selection sortΘ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)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 rownoyesmoving a number costs far more than comparing two, and the row is short
shell sortΘ(n log⁡2 n)\Theta(n \,\log_2\, n)not known; between Ω(n log⁡2 n)\Omega(n \,\log_2\, n) and O(n2)O(n^2)not worked out here; between Ω(n log⁡2 n)\Omega(n \,\log_2\, n) and O(n2)O(n^2)a few spare places (the gap, the number picked up, its place), the same however long the rownoyesyou 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Θ(n log⁡2 n)\Theta(n \,\log_2\, n)Θ(n log⁡2 n)\Theta(n \,\log_2\, n)Θ(n2)\Theta(n^2)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 rownonoyou 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 sortat most O(n log⁡2 n)O(n \,\log_2\, n), as every row of different numbers averages that over the picks; a row with equal numbers can cost a little lessΘ(n log⁡2 n)\Theta(n \,\log_2\, n)Θ(n2)\Theta(n^2), a row of equal numbers, whatever the picks; no run costs more, since a pair meets at most oncea 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 rownonoyou want quick sort without a dear row in order, and equal items need not keep their order
merge sortΘ(n log⁡2 n)\Theta(n \,\log_2\, n)Θ(n log⁡2 n)\Theta(n \,\log_2\, n)Θ(n log⁡2 n)\Theta(n \,\log_2\, n)a second row as long as the rowyesnoyou need comparisons growing no faster than n log⁡2 nn \,\log_2\, n on every row, equal items kept in order, or a file too big for memory sorted
heap sortΘ(n)\Theta(n), every number the sameΘ(n log⁡2 n)\Theta(n \,\log_2\, n), shuffled; measured, no formula worked out hereΘ(n log⁡2 n)\Theta(n \,\log_2\, n), the dearest row; this page finds it only for short rows, by trying every ordera few places, however long the rownoyesyou need a sort with no slow row and no second row, and equal items need not keep their order
counting sortΘ(n)\Theta(n) steps, every row alikeΘ(n)\Theta(n) stepsΘ(n)\Theta(n) steps, every row alikea second row of n places and 10 talliesyesnothe numbers are small whole numbers, such as ratings from 0 to 9, and equal ones must keep their order
radix sortΘ(n)\Theta(n) placings, every number below 10Θ(n)\Theta(n) placings, between the best and the worst, as every order costs the sameΘ(n)\Theta(n) placings, three passes for numbers up to 999a second row of n places and 10 talliesyesnomany 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.

race them on one row of your own