Heap sort
Level 3 of 3
how it works
The idea in a few sentences and three pictures, then one row sorted step by step.
Heap sort reads the row as a tree: the number in place has its children in places and . Take the row 42, 7, 31, 18, 56, 23, 12, the first picture. First heap sort builds a heap: it sifts places 3, 2 and 1, in that order. The row becomes 56, 42, 31, 18, 7, 23, 12, the second picture: 56 sits over 42 and 31, and 42 over 18 and 7. No number sits under a smaller one, and that is a heap. Then heap sort swaps the top, the biggest still in the heap, with the last number of the heap, where it stays. The heap is one smaller, the new top sifts down, and this repeats until one number is left.
| the row | 42, 7, 31, 18, 56, 23, 12 |
|---|---|
| after top 1 | 12, 42, 31, 18, 7, 23, 56 |
| after top 2 | 23, 18, 31, 12, 7, 42, 56 |
| after top 3 | 7, 18, 23, 12, 31, 42, 56 |
| after top 4 | 12, 18, 7, 23, 31, 42, 56 |
| after top 5 | 7, 12, 18, 23, 31, 42, 56 |
| after top 6 | 7, 12, 18, 23, 31, 42, 56 |
step 2 of 53
Sift 31 down: while a child is bigger, swap it with its bigger child.
what it remembers to know where it is
- numbers in the heap
- 7
- number sifting down
- 31
- at place
- 3
step 9 of 53
Swap 7 down with its bigger child, 56.
what it remembers to know where it is
- numbers in the heap
- 7
- number sifting down
- 7
- at place
- 2
step 14 of 53
Swap 42 down with its bigger child, 56.
what it remembers to know where it is
- numbers in the heap
- 7
- number sifting down
- 42
- at place
- 1
step 18 of 53
Every number is now at least as big as its children, so the biggest, 56, is at the top.
what it remembers to know where it is
- numbers in the heap
- 7
- number sifting down
- not yet
- at place
- not yet
Where it says not yet, nothing is held there at that moment.
step 19 of 53
Swap the top, 56, the biggest left, to the end of the heap, where it stays for good.
what it remembers to know where it is
- numbers in the heap
- 6
- number sifting down
- not yet
- at place
- not yet
Where it says not yet, nothing is held there at that moment.
step 26 of 53
Swap 12 down with its bigger child, 18.
what it remembers to know where it is
- numbers in the heap
- 6
- number sifting down
- 12
- at place
- 2
step 53 of 53
In order, after 19 comparisons and 14 swaps.
what it remembers to know where it is
- numbers in the heap
- 0
- number sifting down
- not yet
- at place
- 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
- read the row as a tree: a number's children sit at twice its place and one morePython line 13
- for each parent, from the last back to the first, sift it downPython lines 26 and 27
- the biggest is now at the top; repeat until one number is left in the heapPython line 28
- swap the top with the last number of the heap, where it staysPython line 29
- sift the new top downPython line 30
- sifting down: while a child is bigger, swap the number with its bigger childPython lines 12, 16, 17, 18, 19, 20, 22 and 23
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 heap_sort(numbers):
"""Put a list of numbers in order, smallest first, in the list itself.
Read the list as a tree, and build a heap: no number may sit under a
smaller one. Then swap the top, the biggest still in the heap, with the
heap's last number, where it stays, and let the new top sift down. Python
counts places from 0, so the children of place p are 2p + 1 and 2p + 2.
"""
def sift(place, size):
"""Sift the number at place down the heap of the first size numbers."""
while True:
first = 2 * place + 1 # its first child; the second is next
if first >= size: # no child in the heap: it stops
return
big = place # the biggest so far: the number itself
for child in (first, first + 1): # each child in the heap
if child < size and numbers[child] > numbers[big]:
big = child
if big == place: # no child is bigger: it stops
return
numbers[place], numbers[big] = numbers[big], numbers[place]
place = big # one level down
size = len(numbers)
for place in range(size // 2 - 1, -1, -1): # each parent, last first
sift(place, size)
for end in range(size - 1, 0, -1): # until one number is left
numbers[0], numbers[end] = numbers[end], numbers[0] # top to the end
sift(0, end) # the heap is one smaller: sift the new top down
return numbers
how it scales
How the comparisons grow, from ten numbers to a million.
Each step across or up is ten times the one before, a log-log plot. The line for equal numbers, , climbs about one step up for each step across from 100 numbers on: ten times the numbers, about ten times the comparisons. From 10 to 100 it is more, 24 to 294. The shuffled points climb more than one step each time, by less as they grow. Each tenfold step in numbers multiplies their comparisons by about 27, 16, 14, 13 and 12, from 10 numbers up to a million.
comparisons, ten times more at each mark up
, how many numbers are in the row, ten times more at each mark to the right
- shuffled, the average over every order of different numbers, each order as likelypoints only, each measured by a real run, since no single formula for the comparisons of this row is worked out here
- every number the same, the cheapest rowsolid where measured by running it; dashed where its formula carries it on to ten millionslope 1: ten times n, 10 times the work
| comparisons | time | |
|---|---|---|
| 1,000 | 2,994 | about 3 microseconds |
| 100,000 | 299,994 | about 300 microseconds |
| 10,000,000 | 29,999,994 | about 30 milliseconds |
The comparisons 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 heap sort's comparisons yourself, as the plot on this page does.
Run it with Python 3.12 or later. For each size n it prints the comparisons
on a row of numbers all the same, the cheapest row, and the average over 20
shuffles. The counts are the same on any computer; only the time differs.
"""
import random
class Counted:
"""A number that counts every comparison made with it."""
comparisons = 0
def __init__(self, value):
self.value = value
def __gt__(self, other):
Counted.comparisons += 1
return self.value > other.value
def heap_sort(numbers):
"""Put a list of numbers in order, smallest first, in the list itself.
Read the list as a tree, and build a heap: no number may sit under a
smaller one. Then swap the top, the biggest still in the heap, with the
heap's last number, where it stays, and let the new top sift down. Python
counts places from 0, so the children of place p are 2p + 1 and 2p + 2.
"""
def sift(place, size):
"""Sift the number at place down the heap of the first size numbers."""
while True:
first = 2 * place + 1 # its first child; the second is next
if first >= size: # no child in the heap: it stops
return
big = place # the biggest so far: the number itself
for child in (first, first + 1): # each child in the heap
if child < size and numbers[child] > numbers[big]:
big = child
if big == place: # no child is bigger: it stops
return
numbers[place], numbers[big] = numbers[big], numbers[place]
place = big # one level down
size = len(numbers)
for place in range(size // 2 - 1, -1, -1): # each parent, last first
sift(place, size)
for end in range(size - 1, 0, -1): # until one number is left
numbers[0], numbers[end] = numbers[end], numbers[0] # top to the end
sift(0, end) # the heap is one smaller: sift the new top down
return numbers
def cheapest(n):
"""n numbers all the same: every sift stops at once."""
return [1] * n
def count(row):
"""How many comparisons heap sort makes on this row."""
Counted.comparisons = 0
heap_sort([Counted(x) for x in row])
return Counted.comparisons
if __name__ == "__main__":
random.seed(1)
print(f"{'n':>6} {'all the same':>13} {'shuffled':>10}")
for n in [10, 100, 1000, 10000]:
shuffles = []
for _ in range(20):
row = list(range(1, n + 1))
random.shuffle(row)
shuffles.append(count(row))
print(f"{n:>6} {count(cheapest(n)):>13} {sum(shuffles) / 20:>10.1f}")
best, worst and average
The row decides the cost. Equal numbers are the cheapest: every sift stops at once. From 4 numbers on, shuffled rows cost more on average. This page works out no formula for their average, but section 4 finds how it grows. There is no worst column: this page names no dearest row. The table finds the dearest order of short rows by trying every order, and section 4 bounds its cost for every length.
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 the same, the cheapest row | averageshuffled, the average over every order of different numbers, each order as likely | |
|---|---|---|
| comparisons | , from 3 numbers on | from 3 numbers on, between and ; at 7, between 15 and 32 |
| how the comparisons grow in each case | ||
| tight, | ||
| ceiling, | ||
| floor, | ||
extra memory: a few places, however long the row
Extra memory is the room it needs on top of the input itself, while it runs.
| numbers | orders tried | cheapest order | dearest order | average order | all equal |
|---|---|---|---|---|---|
| 5 | 120 | 9 | 12 | 10.95 | 9 |
| 6 | 720 | 12 | 17 | 15.13 | 12 |
| 7 | 5,040 | 16 | 22 | 19.79 | 15 |
| 8 | 40,320 | 21 | 29 | 25.81 | 18 |
where the formula comes from
Each count above, worked out in small steps. Every step names the line it uses, and each 'where does this come from?' box opens a fact from another page.
the move: one round each
the line:sift place its children comparisons building 2 4 and 5 2 building 1 2 and 3 2 heap of 4 1 2 and 3 2 heap of 3 1 2 and 3 2 heap of 2 1 2 1 in all 9 Heap sort on 8, 8, 8, 8, 8, sift by sift. 4 comparisons build the heap, and 5 sort it. why it is allowed: With equal numbers no child is bigger, so every sift stops after its first round. The picture counts 8, 8, 8, 8, 8.
the move: count the building
the line:why it is allowed: Each place with a child is sifted once, and every place but place 1 is the child of one place. So comparisons; 4 for 5 numbers.
guess it, thenthe move: count the sorting
the line:why it is allowed: The tops of heaps of numbers down to 2 are sifted. The heaps of 3 or more give their top 2 children, as the heaps of 4 and 3 in the picture. The heap of 2 gives 1.
guess it, thenthe move: add them
the line:why it is allowed: is the comparisons on equal numbers, by lines 2 and 3.
guess it, thenthe move: multiply out
the line:why it is allowed: Twice is .
guess it, thenthe move: gather
the line:why it is allowed: The 1s cancel. For 5 numbers, 4 + 4 + 1 is 9, as in the picture.
guess it, thenthe move: no row costs fewer
the line:why it is allowed: On any row, every sift compares at least the children of its place. Lines 2 and 3 count only those.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 6, taking 6 away.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 8, stays under 3 times .
guess it, thenthe move: a floor
the line:why it is allowed: is plus , and is at least 0 from 3 on. At 5, 9 is 5 + 4.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 10.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 9 and 11 bound by the same growth, .
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
the move: price a round
the line:round compares then 1 42 with 12, 31 with 42 swap 12 with 42 2 18 with 12, 7 with 18 swap 12 with 18 Sifting 12 down a heap of 6, whose height is 2. Two rounds of 2 comparisons; then 12 has no child, and it stops. why it is allowed: The picture sifts 12 in the first six places of 12, 42, 31, 18, 7, 23, 56. That is the row just after the first top swap, before the sift mends it.
the move: count the rounds
the line:A heap of 7 has levels 0, 1 and 2, so height 2. Only the marked places, on levels 0 and 1, have children, so only they can hold a round. why it is allowed: A round needs a child below, and the deepest level has none. So only the levels above it can hold a round.
guess it, thenthe move: smaller heaps are no taller
the line:why it is allowed: Taking the last place off never adds a level. The heap of 6 in line 1's picture has height 2, as 7 does.
guess it, thenthe move: count the sifts
the line:part sifts rounds each at most comparisons at most building 3 2 12 sorting 5 2 20 in all 8 32 For 7 numbers: 3 parents and 5 tops, each sift at most 2 rounds of 2 comparisons. That makes 32. why it is allowed: A parent is a place with a child. Sorting sifts the top after every swap but the last, which leaves one number. The picture counts them for 7.
guess it, thenthe move: multiply
the line:why it is allowed: is a count no row goes past, by lines 1 to 4. is the height and the parents. For 7, is 2 and is 3.
guess it, thenthe move: name the height
the line:where does this come from?
question 3 of the page trees and their wordsA tree of places kept in a row. What is its height? the move: the last place is the deepest
the line:why it is allowed: The row fills the levels in order, so its last place, place , is on the last level. So the height is the depth of place .
the move: each step up halves
the line:From place 10 up to the root, place 1, each step halving and rounding down, 10, 5, 2, 1, the steps counted beside them. Three steps, so place 10 has depth 3. why it is allowed: By question 2 of the page trees and their words, the parent of place is place . From place 10 the steps up go 5, 2, 1.
the move: count the steps
the line:why it is allowed: Let be the height. By lines 1 and 2 it is the steps from place up to place 1, each one halving, rounding down.
the move: use halving and logarithms
the line:where does this come from?
question 2 of the page halving and logarithmsHalve , rounding down each time. How many halvings reach 1? the move: name the log rounded down
the line:Rounding down, 11 halves to 5, 2 and 1. In blue, each log rounded down: 3, 2, 1, 0, one less at each halving. why it is allowed: Here is the number halved, from 2 up; for 11, is 3.
the move: rounding down never adds
the line:why it is allowed: For 11, 3 is at most 3.46.
the move: rounding down takes off less than 1
the line:why it is allowed: For 11, 4 is above 3.46.
the move: raise 2 to each side of line 2
the line:why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.
the move: 2 to the log of n is n
the line:why it is allowed: By what a log is. For 11, 8 is at most 11.
the move: raise 2 to each side of line 3
the line:why it is allowed: As in line 4. For 11, about 11 is below 16.
the move: 2 to the log of n is n
the line:why it is allowed: As in line 5. For 11, 11 is below 16.
the move: halve both sides of line 5
the line:why it is allowed: For 11, 4 is at most 5.5, which is not whole.
the move: write the half as a power
the line:why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.
the move: round the half down
the line:why it is allowed: is the half rounded down, 5 for 11. A whole number at most 5.5 is at most 5. The left side, 4 for 11, is whole, as is at least 1 for from 2 up.
the move: halve both sides of line 7
the line:why it is allowed: For 11, 5.5 is below 8.
the move: write the half as a power
the line:why it is allowed: As in line 9. Half of 16 is 8.
the move: round the half down
the line:why it is allowed: Rounding down never adds. For 11, 5 is below 8.
the move: take the log of line 10
the line:why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, the log of 4 is 2, at most about 2.32.
the move: take the log of line 13
the line:why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, about 2.32 is below 3.
the move: round the log down
the line:the number at least below its log rounded down 11 8 16 3 5 4 8 2 2 2 4 1 1 1 2 0 Each halving takes 1 off the log rounded down. why it is allowed: By lines 15 and 17, the log of the half is at least one less than and below , so it rounds down to one less. For 11, about 2.32 rounds down to 2.
the move: use line 18 again and again
the line:why it is allowed: While the log rounded down is 1 or more, the number is at least 2 to the power 1, which is 2. So line 18 takes 1 off each halving, down to 0 after . For 11, 3, 2, 1, 0.
the move: a log rounded down to 0 belongs to 1
the line:why it is allowed: Such a log is at least 0 and below 1, so its number is at least 1 and below 2. It is 1.
why it is allowed: The page on halving shows that halving , rounding down, reaches 1 after halvings. For 10, the biggest power of 2 not past it is 8, three 2s multiplied, so 3 halvings, as in the picture. A tree of 1 place needs none, and the log of 1 is 0.
the move: read it
the line:why it is allowed: 10 lies between 8 and 16, so its log rounded down is 3. 1,000 lies between 512 and 1,024, so 9. Doubling the places adds 1 to the height. 20 places end at place 20, the first child of place 10 by question 2 of the page trees and their words, one level below where 10 places end.
why it is allowed: The box 'where does this come from?' under this line shows a tree of places kept in a row has height .
guess it, thenthe move: count the parents
the line:where does this come from?
question 2 of the page trees and their wordsFrom place of a tree kept in a row, which places lie one step down, and which one step up? the move: find where a level starts
the line:The tree of 10 places. Its levels start at places 1, 2, 4 and 8, each block in the row starting where the one before ends. why it is allowed: Say place is on level . No level starts until the one above it is used up, so the levels above level are full. For level 3, levels 0 to 2 hold 1, 2 and 4, which is 7 places, so level 3 starts at place 8. In general the levels above form a full tree whose height is one less than . By question 1 of the page trees and their words it holds one place fewer than , so level starts at place . Level 0 is the root alone, at place 1, and 2 to the power 0 is 1.
the move: count what comes before the first child
the line:Places 2 and 3 take their children in turn, 4 and 5, then 6 and 7. So the first child of place 3 comes after the two of place 2. why it is allowed: Let be the place of the first child of . On level , places come before . The row fills level from its left end, each place's children in turn, so each place before has its two children there before 's first. That level starts at place . For place 3, one place comes before it on level 1, so its first child is 4 + 2, place 6.
the move: multiply out
the line:why it is allowed: Twice is twice less twice .
the move: tidy the power
the line:why it is allowed: Twice is .
the move: cancel
the line:why it is allowed: The first and last terms cancel. For place 3, twice 3 is 6, as line 2 found.
the move: the second child is next
the line:why it is allowed: The two children stand side by side. For place 3, places 6 and 7. A place whose first child would fall past the end of the row is a leaf.
the move: halve a first child
the line:why it is allowed: The floor brackets round down, as on the page on halving. Half of is exactly, so rounding changes nothing.
the move: halve a second child
the line:why it is allowed: Half of is and a half, which rounds down to .
the move: read it
the line:why it is allowed: Every place but the root is the first or the second child of some place, twice it or one more, by lines 5 and 6. Halving it and rounding down gives that place back, by lines 7 and 8. For places 6 and 7, place 3.
why it is allowed: That box finds each place's children. Place is a parent when twice is at most , so places 1 to . For 7, is 2 × 2 × (3 + 5), which is 32.
guess it, thenthe move: a simpler bracket
the line:why it is allowed: Rounding down makes no bigger, and taking 2 away makes less. At 7, 3 + 5 is 8, at most 3.5 + 7, which is 10.5.
guess it, thenthe move: add the halves
the line:why it is allowed: Half of and make 3 halves of .
guess it, thenthe move: multiply
the line:why it is allowed: Twice 3 halves is 3.
guess it, thenthe move: drop the rounding
the line:why it is allowed: Rounding down makes no bigger.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 11, stays under 3 times .
guess it, thenthe move: read it
the line:why it is allowed: Lines 1 to 4 hold on every row. Of the 5,040 orders of 7 numbers, the dearest costs 22, under 32.
guess it, then
try it with a small : at , line 5 gives , which you can work out by hand
the move: name it
the line:why it is allowed: This page finds by trying every order, for short rows. By the question on the ceiling, is at most .
the move: the fewest doublings
the line:why it is allowed: counts the doublings 1, 2, 4 and on until they reach the product. At 7 the product is 5,040; 4,096 falls short, 8,192 reaches it, so 13. is a floor under , which is 22 at 7.
guess it, thenthe move: count the orders
the line:where does this come from?
question 1 of the page the sorting floorIn how many orders can different numbers stand? the move: the first place
the line:why it is allowed: Any of the numbers can stand first, so any of 3 for 1, 2 and 3.
the move: the second place
the line:first second third 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 The 6 orders of 1, 2 and 3, place by place. why it is allowed: From 2 numbers on, each first choice leaves for the second place. For 1, 2 and 3, 3 times 2 is 6, and the number left fills the third.
the move: on to the last
the line:why it is allowed: Let be the number of orders. As in line 2, each place has one choice fewer, down to 1, and the choices multiply. For 3, 3 × 2 × 1 is 6.
the move: n factorial
the line:why it is allowed: down to 1, multiplied, is written , read factorial.
the move: read it
the line:why it is allowed: By line 3, one number more adds a factor. 3 numbers stand in 6 orders, 4 in 24 and 5 in 120.
why it is allowed: The box under this line shows that different numbers stand in orders, written .
guess it, thenthe move: name the sorting floor
the line:where does this come from?
question 3 of the page the sorting floorAt least how many comparisons does a comparing sort's worst row of different numbers take? the move: answers decide
the line:the row answers moves it ends as 1, 3, 2 no yes no swap the last two 1, 2, 3 10, 30, 20 no yes no swap the last two 10, 20, 30 Bubble sort walks left to right, asking each pair of neighbours 'is the left bigger?' and swapping on a yes. Each walk is one pair shorter, until one swaps nothing or no pair is left. why it is allowed: A comparing sort with no random choices learns only from its answers, so its next comparison and move hang only on its answers so far. So rows giving the same answers get the same comparisons and moves, and stop together.
the move: one list per order
the line:why it is allowed: Pretend two different orders of the same numbers gave the same answers. By line 1 they get the same moves, carrying what stands at each place to the same place on both. So they end different, at most one in order: a swap puts 2, 1 in order but not 1, 2. Yet the sort puts every row in order, so each order gets its own list.
the move: fill out short lists
the line:Short lists carried down to 3 answers by dotted yeses. No no, for 1, 2, 3, begins no other list, so no no yes is new. why it is allowed: Let be the most comparisons on any order. Fill each shorter list out to answers with pretend yeses, so all have the one length line 4 counts. Filling adds only at the end. And no list begins another: if one did, by line 1 its row and the longer one's would stop together, so the longer would not go on. So the filled lists stay different.
the move: count both
the line:orders the floor its lists 3 6 3 8 4 24 5 32 5 120 7 128 8 40,320 16 65,536 12 479,001,600 29 536,870,912 One answer fewer gives half the lists, too few. why it is allowed: Each answer doubles the lists, so answers make , against orders. By lines 2 and 3, lists are at least as many as orders. For 4, the 16 lists of 4 answers are too few for 24.
the move: name the floor
the line:why it is allowed: Let be the fewest doublings of 1 that reach or pass it. For 4, four doublings reach only 16, short of 24, and the fifth, 32, passes it.
the move: at least
the line:why it is allowed: By line 4, doublings of 1 reach , and by line 5 no fewer than do. So is at least .
the move: halving and logarithms
the line:where does this come from?
question 4 of the page halving and logarithmsHalve , rounding up each time. How many halvings reach 1? the move: name the log rounded up
the line:why it is allowed: Here is the number halved, from 2 up. For 11, is 4.
the move: rounding up never takes off
the line:why it is allowed: The bracket is . For 11, 3.46 is at most 4.
the move: rounding up adds less than 1
the line:why it is allowed: For 11, 3 is below 3.46.
the move: raise 2 to each side of line 2
the line:why it is allowed: As in line 4 of question 2 of the page halving and logarithms.
the move: 2 to the log of n is n
the line:why it is allowed: As in line 5 of question 2 of the page halving and logarithms. For 11, 11 is at most 16.
the move: raise 2 to each side of line 3
the line:why it is allowed: As in line 4.
the move: 2 to the log of n is n
the line:why it is allowed: As in line 5. For 11, 8 is below 11.
the move: halve both sides of line 5
the line:why it is allowed: Halving both sides keeps it true.
the move: write the half as a power
the line:why it is allowed: As in line 9 of question 2 of the page halving and logarithms. For 11, 5.5 is at most 8.
the move: round the half up
the line:why it is allowed: is the half rounded up: the smallest whole number at or above it, 6 for 11. The right side, 8 for 11, is whole, as is at least 1. By line 9 it is at or above the half, so no smaller than the half rounded up.
the move: halve both sides of line 7
the line:why it is allowed: Halving both sides keeps it true.
the move: write the half as a power
the line:One half, a step left of 1, is 2 to the power minus 1. why it is allowed: As in line 9 of question 2 of the page halving and logarithms. For 11, 4 is below 5.5. For 2, is 1, so this is 2 to the power minus 1, one half, as drawn.
the move: round the half up
the line:why it is allowed: Rounding up never takes off. For 11, 4 is below 6.
the move: take the log of line 10
the line:why it is allowed: As in line 14 of question 2 of the page halving and logarithms.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, the log of 6, about 2.58, is at most 3.
the move: take the log of line 13
the line:why it is allowed: As in line 14. For 2, the log of one half is minus 1, below the log of 1, which is 0.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, 2 is below about 2.58.
the move: round the log up
the line:the number above at most its log rounded up 11 8 16 4 6 4 8 3 3 2 4 2 2 1 2 1 1 1/2 1 0 Each halving takes 1 off the log rounded up. why it is allowed: By lines 15 and 17, the log of the half is above two less than and at most one less, so it rounds up to one less. For 11, about 2.58 rounds up to 3.
the move: use line 18 again and again
the line:why it is allowed: While the log rounded up is 1 or more, the log is above 0, so the number is above 1: at least 2. So line 18 takes 1 off each halving, and the number stays 2 or more until exactly halvings. For 11, 4, 3, 2, 1, 0.
the move: a log rounded up to 0 belongs to 1
the line:why it is allowed: Such a log is at most 0, so its number is at most 1, and so is 1.
the move: read lines 5 and 7 as doublings
the line:why it is allowed: Doubling 1 gives the powers of 2. By line 5, doublings get to or past ; by line 7, one fewer stay below. For 11, 8 stays below and 16 gets past.
the move: count merge sort's rounds
the line:Round 2 merges 5, 9 and 2, 7 into 2, 5, 7, 9. after round runs start 11 1 6 2 3 3 2 4 1 The first run holds 1, 2, 4, 8, then all 11: four rounds. The count of runs is 11 halved, rounding up. why it is allowed: Merge sort starts from runs of 1, whatever the row. Each round merges neighbouring runs in pairs, so the first run doubles, the last round taking only what is left. By line 21 it holds all after rounds, not fewer.
the move: read it
the line:why it is allowed: By lines 20 and 22, one count gives both the halvings and merge sort's rounds. For 11, four of each.
why it is allowed: The fewest doublings of 1 reaching a number from 2 up are its log to base 2 rounded up: 5 for 24. The box under this line, from the page halving and logarithms, shows it at its lines 5 and 7.
the move: read it
the line:why it is allowed: By lines 6 and 7, every comparing sort that makes no random choices makes at least comparisons on its worst row of . It is only a floor: for 12 numbers it is 29, yet the best sort for 12 needs 30 on its worst row. An outside result, and no line here rests on it (Mark Wells, 1965; Knuth, The Art of Computer Programming, volume 3, section 5.3.1).
why it is allowed: The box under this line counts those doublings as the log of rounded up, the sorting floor.
guess it, thenthe move: the dearest needs
the line:numbers floor dearest ceiling 5 7 12 20 6 10 17 28 7 13 22 32 8 16 29 60 Every order of 1 to n tried. The dearest always lies between the floor and the ceiling. why it is allowed: The box at line 4 shows it, at its line 6. No comparing sort's dearest row costs fewer. The picture tries it.
guess it, thenthe move: write the log as a sum
the line:where does this come from?
question 4 of the page the sorting floorHow does grow? the move: name the log
the line:why it is allowed: Let be the log to base 2 of , for from 4 as lines 12 and 15 need. For 8, about 15.30.
the move: factors at most n
the line:why it is allowed: multiplies whole numbers, each at most . Putting for each gives , read to the power , no smaller. For 8, 40,320 against 16,777,216.
the move: bigger number, bigger log
the line:why it is allowed: On a number line of powers of 2, 16 stands right of 8, and its log, 4, right of 8's log, 3. Any two numbers go the same way, as the page halving and logarithms shows, so line 2 holds for the logs.
the move: into line 1
the line:why it is allowed: By line 3, as is the log of .
the move: log of a power
the line:why it is allowed: For 8: 8 is three 2s, so 8 to the power 8 is 24 2s, since multiplying powers of 2 adds the powers. Each number is 2 raised to its log, so the log of a product adds the logs. is a product of factors, each , so its log is times .
the move: keep the top half
the line:why it is allowed: Drop the factors 1 to half of rounded down; each is at least 1, so the product gets no bigger. Half of rounded down and rounded up add up to , as 3 and 4 for 7. So are kept, read half of rounded up.
the move: half of n for each
the line:for 8 multiplied 8 down to 1 40,320 8 × 7 × 6 × 5 1,680 4 × 4 × 4 × 4 256 Drop 4 down to 1, then put 4, half of 8, for each factor kept. why it is allowed: Each kept factor is above half of , so putting half of for each gives no bigger product.
the move: bigger number, bigger log
the line:why it is allowed: As in line 3.
the move: into line 1
the line:why it is allowed: By line 8, as is the log of .
the move: log of a power
the line:why it is allowed: As in line 5. For 8, 4 times 2.
the move: rounding up
the line:why it is allowed: Rounding up makes no smaller, as 3.5 goes up to 4.
the move: log of half of n
the line:why it is allowed: From 4 on, half of is at least 2, whose log is 1; by line 3, bigger numbers have bigger logs.
the move: drop the rounding
the line:why it is allowed: By line 11, half of is at most , as 3.5 against 4 for 7. Each multiplies the log of half of , above zero by line 12, so putting half of in line 10 makes it no bigger.
the move: halving takes 1 off
the line:why it is allowed: is 2 times half of , and the log of 2 is 1. So by line 5 the log of half of is . For 8, the log of 4 is 3 less 1.
the move: a fact about n
the line:why it is allowed: From 4 on, is at least 2, so half of it is at least 1, and taking 1 away leaves at least half. For 8, 1.5 against 2.
the move: the smaller part
the line:why it is allowed: By line 15, putting half of for in line 14 makes it no bigger, as in line 13. For 8, 4 times 1.5, 6.
the move: multiply out
the line:- the log of
- a quarter of
The log of between the other two, from 1 to 10 numbers. At 8: 24, about 15.30, and 6. why it is allowed: Half of times half of .
the move: a ceiling
the line:why it is allowed: , read is in big O of , says a fixed multiple of it is a ceiling of . By line 5, 1 times it, from 4 on.
the move: a floor
the line:why it is allowed: , read big omega, says a fixed multiple, above zero, is a floor of , a floor on growth, not the sorting floor. By line 17, a quarter, from 4 on.
the move: both
the line:why it is allowed: , read big theta, says both, by lines 18 and 19. grows like .
why it is allowed: The log of a product adds the logs, as the box under this line uses. The log of 4 × 8 = 32 is 5, which is 2 + 3, the logs of 4 and 8.
guess it, thenthe move: rounding up makes no smaller
the line:why it is allowed: By line 6.
guess it, thenthe move: a floor
the line:numbers a quarter of it 4 2 8 2 5 8 3 24 6 16 16 4 64 16 45 F against a quarter of n log2 n, at 4, 8 and 16 numbers. F is always the bigger. why it is allowed: The box at line 6 shows the sum is at least this from 4 on, at its line 17. The picture tries it.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 8, the fixed multiple a quarter, from 4 on.
guess it, thenthe move: a floor for
the line:why it is allowed: By lines 5 and 9.
guess it, thenthe move: a ceiling for
the line:why it is allowed: By line 1, is at most , which the question on the ceiling finds is in .
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 10 and 11 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: few short lists
the line:answers lists 0 1 1 2 2 4 fewer than 3 7 Lists of 0, 1 and 2 answers, 7 in all. So at most 7 of the 24 orders of 4 numbers finish in fewer than 3 comparisons. Heap sort takes 6 or 7 on each. why it is allowed: This question sets up the average. For 4 numbers, take as 3. Only 7 lists have fewer than 3 answers, under half of 24.
the move: one list each
the line:comparisons lists enough for 24 orders? 1 2 no 2 4 no 3 8 no 4 16 no 5 32 yes Each comparison doubles the lists. 4 numbers stand in 24 orders, each needing its own list, so some order takes at least 5 comparisons. why it is allowed: Two orders given the same list of answers are moved the same way, so one would end out of order. The box at line 4 of the question on the dearest row shows it, at its line 2.
guess it, thenthe move: choose k
the line:why it is allowed: For 4 numbers, the log of 24 is about 4.58, so is 3.
guess it, thenthe move: count the short lists
the line:why it is allowed: is the lists of fewer than answers.
guess it, thenthe move: lists of each length
the line:where does this come from?
question 2 of the page the sorting floorHow many different lists of yes-or-no answers are there? the move: two answers
the line:why it is allowed: Here counts answers, not numbers. Each comparison answers yes or no.
the move: each answer doubles
the line:answers the lists how many 0 the empty list 1 1 yes; no 2 2 yes yes; yes no; no yes; no no 4 Each answer more doubles the lists, 1, 2, 4. why it is allowed: Each list goes on with a yes or a no, so each answer doubles the lists. No answers make one list, the empty one.
the move: double n times
the line:why it is allowed: Let be the number of lists of answers. By line 2, 1 doubled times. For 3, 2 × 2 × 2 is 8.
the move: a power
the line:why it is allowed: 2s multiplied are written , read 2 to the power . 2 to the power 0 is 1.
the move: read it
the line:why it is allowed: By line 4, one answer more is one 2 more: 4 answers make 16 lists and 5 make 32.
why it is allowed: The box under this line shows each answer doubles the lists. There is 1 list of no answers, 2 of one, 4 of two.
guess it, thenthe move: add the doublings
the line:where does this come from?
question 1 of the page doubling and halving sumsAdd . What is the total? the move: name the sum
the line:why it is allowed: Let be the doubling sum up to . For as 3, it is 1 + 2 + 4 + 8.
the move: double both sides
the line:why it is allowed: Each term doubled is the next one along, and becomes . Doubling both sides keeps them equal. The plan: the two sums share all but their ends.
the move: take line 1 away
the line:why it is allowed: Take from both sides of line 2. On the right, write out as line 1 does.
the move: tidy the left side
the line:why it is allowed: Two lots of less one lot is one lot.
the move: cancel what both brackets share
the line:when n is 3 1 2 4 8 16 first bracket 2 4 8 16 second bracket 1 2 4 8 left over take away 1 16 Line 4 when is 3. The 2, 4 and 8 cancel, leaving 16 less 1, which is 15. why it is allowed: Every term from 2 to is in both brackets and cancels, leaving less 1.
the move: the terms before the last
the line:why it is allowed: The sum of line 5 stopping one term sooner: line 5 with for . For 3, it is 1 + 2 + 4. At as 0 no terms are left, adding to 0, and 2 to the power 0, less 1, is 0 too.
the move: tidy the power
the line:why it is allowed: One more than is . For 3, 1 + 2 + 4 is 7, one less than 8.
the move: read it
the line:why it is allowed: By lines 6 and 7, the terms before the last add to one less than it: the last outweighs them all. By line 5 the sum is under , twice the last.
why it is allowed: The box under this line adds them. For 4 numbers, 1 + 2 + 4 is 7, one less than 8.
guess it, thenthe move: put k in
the line:why it is allowed: By line 3.
guess it, thenthe move: halve the power
the line:where does this come from?
question 2 of the page halving and logarithmsHalve , rounding down each time. How many halvings reach 1? the move: name the log rounded down
the line:Rounding down, 11 halves to 5, 2 and 1. In blue, each log rounded down: 3, 2, 1, 0, one less at each halving. why it is allowed: Here is the number halved, from 2 up; for 11, is 3.
the move: rounding down never adds
the line:why it is allowed: For 11, 3 is at most 3.46.
the move: rounding down takes off less than 1
the line:why it is allowed: For 11, 4 is above 3.46.
the move: raise 2 to each side of line 2
the line:why it is allowed: 2 raised to a bigger power gives a bigger number. For 11, 8 is at most about 11.
the move: 2 to the log of n is n
the line:why it is allowed: By what a log is. For 11, 8 is at most 11.
the move: raise 2 to each side of line 3
the line:why it is allowed: As in line 4. For 11, about 11 is below 16.
the move: 2 to the log of n is n
the line:why it is allowed: As in line 5. For 11, 11 is below 16.
the move: halve both sides of line 5
the line:why it is allowed: For 11, 4 is at most 5.5, which is not whole.
the move: write the half as a power
the line:why it is allowed: Half a power of 2 has one 2 fewer. Half of 8 is 4.
the move: round the half down
the line:why it is allowed: is the half rounded down, 5 for 11. A whole number at most 5.5 is at most 5. The left side, 4 for 11, is whole, as is at least 1 for from 2 up.
the move: halve both sides of line 7
the line:why it is allowed: For 11, 5.5 is below 8.
the move: write the half as a power
the line:why it is allowed: As in line 9. Half of 16 is 8.
the move: round the half down
the line:why it is allowed: Rounding down never adds. For 11, 5 is below 8.
the move: take the log of line 10
the line:why it is allowed: A bigger number has a bigger log. For 11, the log of 4 is at most the log of 5.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, the log of 4 is 2, at most about 2.32.
the move: take the log of line 13
the line:why it is allowed: As in line 14. For 11, the log of 5 is below the log of 8.
the move: the log of a power of 2 is the power
the line:why it is allowed: For 11, about 2.32 is below 3.
the move: round the log down
the line:the number at least below its log rounded down 11 8 16 3 5 4 8 2 2 2 4 1 1 1 2 0 Each halving takes 1 off the log rounded down. why it is allowed: By lines 15 and 17, the log of the half is at least one less than and below , so it rounds down to one less. For 11, about 2.32 rounds down to 2.
the move: use line 18 again and again
the line:why it is allowed: While the log rounded down is 1 or more, the number is at least 2 to the power 1, which is 2. So line 18 takes 1 off each halving, down to 0 after . For 11, 3, 2, 1, 0.
the move: a log rounded down to 0 belongs to 1
the line:why it is allowed: Such a log is at least 0 and below 1, so its number is at least 1 and below 2. It is 1.
why it is allowed: One 2 fewer in a power halves it. The box under this line does the same, at its lines 8 and 9.
guess it, thenthe move: under half of n!
the line:numbers half of less 1 4 24 3 7 11 5 120 5 31 59 6 720 8 255 359 The short lists against half the orders, less 1, for 4, 5 and 6 numbers. why it is allowed: The box at line 8 shows 2 to the power of (the log rounded down, less 1) is at most half the number, at its line 9. At 4, 8 is at most 12, so 7 is at most 11.
guess it, thenthe move: fewer than half are quick
the line:why it is allowed: By lines 2 and 9, as there are orders. For 4 numbers, at most 7 of 24.
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
the move: most orders are slow
the line:why it is allowed: By the question on short lists. For 4 numbers, at least 17 of 24 take 3 or more.
the move: add up the slow ones
the line:why it is allowed: By line 1, more than half the orders add at least each, and no order adds less than 0. For 4 numbers, at least 17 times 3, which is 51.
guess it, thenthe move: a floor for the average
the line:numbers half of average 4 3 1.5 6.5 5 5 2.5 10.95 6 8 4 15.13 7 11 5.5 19.79 8 14 7 25.81 Heap sort's average over every order, against half of k. The floor is low, but the next lines show it grows like the average. why it is allowed: is that total shared out over the orders, so by line 2 it is more than half of . For 4 numbers, 51 over 24 is above 1.5.
guess it, thenthe move: drop the rounding
the line:why it is allowed: Rounding down takes off less than 1, so is at least the log of less 2.
guess it, thenthe move: use the sorting floor's growth
the line:why it is allowed: From 4 on, the log of is at least a quarter of . The box at line 6 of the question on the dearest row shows it, at its line 17.
guess it, thenthe move: halve each part
the line:why it is allowed: Half of a quarter is an eighth, and half of 2 is 1.
guess it, thenthe move: a simpler floor
the line:why it is allowed: From 7 on, is at least 16, so taking 1 off its eighth leaves at least its sixteenth. At 7 it is about 19.65; its eighth less 1 is about 1.46, its sixteenth about 1.23.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 7, the fixed multiple a sixteenth, from 7 on.
guess it, thenthe move: a ceiling from every row
the line:why it is allowed: No row goes past , by the question on the ceiling, so neither does the average.
guess it, thenthe move: read it as a bound
the line:why it is allowed: The question on the ceiling finds under 3 times .
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 8 and 10 bound by the same growth, .
guess it, then
try it on small rows: over every order of 7 numbers, the average is about 19.79
count it yourself
Count every comparison on 2, 2, 2, 2. For 4 numbers the formula gives 3 times 4, less 6, which is 6.
| step | the row | what happens | comparisons | swaps |
|---|---|---|---|---|
| 1 | 2, 2, 2, 2 | There are 4 numbers in a row, drawn below as a tree: the number in place p has its children in places 2p and 2p + 1.2, 2, 2, 2 | 0 | 0 |
| 2 | 2, 2, 2, 2 | Sift 2 down: while a child is bigger, swap it with its bigger child.2, 2, 2, 2 | 0 | 0 |
| 3 | 2, 2, 2, 2 | 2 is not bigger than 2.2, 2, 2, 2 | 1 | 0 |
| 4 | 2, 2, 2, 2 | 2 is at least as big as its children, so it stops here.2, 2, 2, 2 | 1 | 0 |
| 5 | 2, 2, 2, 2 | Sift 2 down: while a child is bigger, swap it with its bigger child.2, 2, 2, 2 | 1 | 0 |
| 6 | 2, 2, 2, 2 | 2 is not bigger than 2.2, 2, 2, 2 | 2 | 0 |
| 7 | 2, 2, 2, 2 | 2 is not bigger than 2.2, 2, 2, 2 | 3 | 0 |
| 8 | 2, 2, 2, 2 | 2 is at least as big as its children, so it stops here.2, 2, 2, 2 | 3 | 0 |
| 9 | 2, 2, 2, 2 | Every number is now at least as big as its children, so the biggest, 2, is at the top.2, 2, 2, 2 | 3 | 0 |
| 10 | 2, 2, 2, 2 | Swap the top, 2, the biggest left, to the end of the heap, where it stays for good.2, 2, 2, 2 | 3 | 1 |
| 11 | 2, 2, 2, 2 | The heap is one smaller. Sift the new top, 2, down.2, 2, 2, 2 | 3 | 1 |
| 12 | 2, 2, 2, 2 | 2 is not bigger than 2.2, 2, 2, 2 | 4 | 1 |
| 13 | 2, 2, 2, 2 | 2 is not bigger than 2.2, 2, 2, 2 | 5 | 1 |
| 14 | 2, 2, 2, 2 | 2 is at least as big as its children, so it stops here.2, 2, 2, 2 | 5 | 1 |
| 15 | 2, 2, 2, 2 | Swap the top, 2, the biggest left, to the end of the heap, where it stays for good.2, 2, 2, 2 | 5 | 2 |
| 16 | 2, 2, 2, 2 | The heap is one smaller. Sift the new top, 2, down.2, 2, 2, 2 | 5 | 2 |
| 17 | 2, 2, 2, 2 | 2 is not bigger than 2.2, 2, 2, 2 | 6 | 2 |
| 18 | 2, 2, 2, 2 | 2 is at least as big as its children, so it stops here.2, 2, 2, 2 | 6 | 2 |
| 19 | 2, 2, 2, 2 | Swap the top, 2, the biggest left, to the end of the heap, where it stays for good.2, 2, 2, 2 | 6 | 3 |
| 20 | 2, 2, 2, 2 | Only 2 is left in the heap, so it is in place too.2, 2, 2, 2 | 6 | 3 |
| 21 | 2, 2, 2, 2 | In order, after 6 comparisons and 3 swaps.2, 2, 2, 2 | 6 | 3 |
In all, 6 comparisons. The formula from section 4, , gives at : the same.
where you meet it
Heap sort is the safety net inside a fast standard sort.
the C++ library's sort
C++ comes with a library, ready made code for common jobs. In the one that comes with the GNU compiler, std::sort splits like quick sort. It picks a number and splits the part into numbers no bigger and numbers no smaller. When its parts go more than twice a heap's height deep, it heap sorts the part. For two million numbers the height is 20, so the limit is 40. So no row makes it slow.
source: the GNU C++ library's bits/stl_algo.h (__sort, __introsort_loop, __partial_sort, __heap_select) and bits/stl_algobase.h (__lg), GCC 15.2.0 as packaged in libstdc++-15-dev 15.2.0-16ubuntu1, read offline
the C++ library's heap tools
The same library offers heap sort's two halves. std::make_heap builds a heap, and std::sort_heap moves the top to the end and mends the heap, again and again. std::partial_sort uses a heap to find the smallest few numbers in order.
source: the GNU C++ library's bits/stl_heap.h (make_heap, sort_heap, __pop_heap, __adjust_heap) and bits/stl_algo.h (partial_sort, __heap_select), GCC 15.2.0, read offline
what is used instead
Heap sort is rarely the first choice. Sebastian Bourges' books, Algorithmic Thinking Without the Jargon, volume two, chapter 24, find it the slowest in practice of three fast sorts: merge sort, quick sort and heap sort. It also lets equal numbers swap. Sorting 5, 5, 1 ends 1, 5, 5 with the second 5 first and the first 5 last. Merge sort keeps them in order. The books put the pairing well: 'Quicksort supplies the speed, heapsort supplies the guarantee, and the combination has both.' (volume two, chapter 24)
how it compares
Heap sort beside the other ways to put a row in order. Its nearest rivals are merge sort, which needs a second row, and quick sort, which is slow on a row already in order.
- 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.