Shell sort
how it works
The idea, three pictures, then one row sorted step by step.
Shell sort puts a row of numbers in order, smallest first. It is insertion sort with long jumps first. A pass with the gap 4 compares each number from place 5 on with the number 4 places to its left. While that one is bigger, the two trade places, a move. The gaps are 1, 4, 13, 40 and on, each three times the one before and 1 more, a list Donald Knuth suggested. The sort uses every gap below the row's length, the largest first, so its last pass is insertion sort. One long move can mend several wrong pairs at once, and make a few. Donald Shell published the sort in 1959. A long move can carry a number past an equal one, so equal numbers may lose their order.
step 1 of 44
There are 8 numbers in a row. Sort the numbers 4 places apart first, then the neighbours.
what it remembers to know where it is
- gap
- not yet
- number being placed
- not yet
- its place now
- not yet
Where it says not yet, nothing is held there at that moment.
step 4 of 44
42 is not bigger than 56, so 56 stops here.
what it remembers to know where it is
- gap
- 4
- number being placed
- 56
- its place now
- 5
step 8 of 44
31 is bigger than 12, so 31 has to make room.
what it remembers to know where it is
- gap
- 4
- number being placed
- 12
- its place now
- 7
step 9 of 44
31 jumps 4 places right to make room, and 12 jumps the same distance left.
what it remembers to know where it is
- gap
- 4
- number being placed
- 12
- its place now
- 3
step 13 of 44
Now the last pass: compare neighbours, as plain insertion sort does.
what it remembers to know where it is
- gap
- 1
- number being placed
- not yet
- its place now
- not yet
Where it says not yet, nothing is held there at that moment.
step 16 of 44
42 jumps one place right to make room, and 7 jumps the same distance left.
what it remembers to know where it is
- gap
- 1
- number being placed
- 7
- its place now
- 1
step 42 of 44
56 jumps one place right to make room, and 49 jumps the same distance left.
what it remembers to know where it is
- gap
- 1
- number being placed
- 49
- its place now
- 7
step 44 of 44
In order, after 18 comparisons and 9 moves.
what it remembers to know where it is
- gap
- not yet
- number being placed
- not yet
- its place now
- 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 gap, from the largest down to 1Python lines 10, 11, 12, 13 and 21
- for each number from the gap onwardsPython line 14
- pick it upPython lines 15 and 16
- while the number a gap to its left is biggerPython line 17
- move that number a gap rightPython lines 18 and 19
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 shell_sort(numbers):
"""Put a list of numbers in order, smallest first, in the list itself.
Sort the numbers a long gap apart first, then a shorter gap, and so on
down to a gap of 1, which is plain insertion sort, on a list the long
gaps have often left nearer to order. The gaps come from a list Knuth
suggested, 1, 4, 13, 40 and on, each three times the last and one more;
it uses every one shorter than the list (just 1 for one number).
"""
gap = 1
while 3 * gap + 1 < len(numbers): # the largest gap shorter than the list, or 1
gap = 3 * gap + 1
while gap >= 1: # each gap, the largest first
for i in range(gap, len(numbers)): # each number with a number a gap to its left
placing = numbers[i] # pick it up
place = i
while place >= gap and numbers[place - gap] > placing: # bigger, a gap to its left
numbers[place] = numbers[place - gap] # move that number a gap right
place -= gap
numbers[place] = placing # put the number down in the room made
gap = (gap - 1) // 3 # the next gap down: 13 gives 4, 4 gives 1, 1 gives 0
return numbers
how it scales
How the comparisons grow, from ten numbers to ten million.
Each step across or up is ten times the one before, a log-log plot. At the long end a row in order climbs a little over one step per step, 11.8 times from a million numbers to ten million. The shuffled points, each the mean of 20 shuffles, climb about 1.2 steps per step from 1,000 numbers on. No dearest row is drawn, since none is known for long rows. The flat line labelled a second marks a billion comparisons, about a second of a computer's work.
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, different numbers in a random order, every order as likelypoints only, each measured by a real run, since no single formula for the comparisons of this row is worked out here
- already in order, a cheapest rowsolid where measured by running it; dashed where its formula carries it on to ten millionslope 1.1: ten times n, about 12 times the work
| comparisons | time | |
|---|---|---|
| 1,000 | 5,457 | about 5.5 microseconds |
| 100,000 | 967,146 | about 967 microseconds |
| 10,000,000 | 139,238,328 | about 139 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 Shell 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 already in order, on a row back to front (not the dearest row, as
section 3 shows), and the average over 20 shuffles. The counts are the same
on any computer; only the time taken 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 shell_sort(numbers):
"""Put a list of numbers in order, smallest first, in the list itself.
Sort the numbers a long gap apart first, then a shorter gap, and so on
down to a gap of 1, which is plain insertion sort, on a list the long
gaps have often left nearer to order. The gaps come from a list Knuth
suggested, 1, 4, 13, 40 and on, each three times the last and one more;
it uses every one shorter than the list (just 1 for one number).
"""
gap = 1
while 3 * gap + 1 < len(numbers): # the largest gap shorter than the list, or 1
gap = 3 * gap + 1
while gap >= 1: # each gap, the largest first
for i in range(gap, len(numbers)): # each number with a number a gap to its left
placing = numbers[i] # pick it up
place = i
while place >= gap and numbers[place - gap] > placing: # bigger, a gap to its left
numbers[place] = numbers[place - gap] # move that number a gap right
place -= gap
numbers[place] = placing # put the number down in the room made
gap = (gap - 1) // 3 # the next gap down: 13 gives 4, 4 gives 1, 1 gives 0
return numbers
def count(row):
"""How many comparisons Shell sort makes on this row."""
Counted.comparisons = 0
shell_sort([Counted(x) for x in row])
return Counted.comparisons
if __name__ == "__main__":
random.seed(1)
print(f"{'n':>6} {'in order':>10} {'back to front':>14} {'shuffled':>10}")
for n in [10, 100, 1000]:
shuffles = []
for _ in range(20):
row = list(range(1, n + 1))
random.shuffle(row)
shuffles.append(count(row))
in_order = count(list(range(1, n + 1)))
back_to_front = count(list(range(n, 0, -1)))
print(f"{n:>6} {in_order:>10} {back_to_front:>14} {sum(shuffles) / 20:>10.1f}")
best, worst and average
In order, each number picked up is compared once and stays put, the fewest any row costs. A shuffled row has no formula known for every length, so the grid gives ceilings and a floor. No dearest row can be named for every length, so no worst case column.
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.
| bestalready in order, a cheapest row | averageshuffled, different numbers in a random order, every order as likely | |
|---|---|---|
| comparisons | , one term for each gap below | not known; at least the best case, and at most |
| moves | none | not known; at most |
| how the comparisons grow in each case | ||
| tight, | not known | |
| ceiling, | ||
| floor, | ||
extra memory: a few spare places (the gap, the number picked up, its place), the same however long the row
Extra memory is the room it needs on top of the input itself, while it runs.
| row of 8 | comparisons | moves |
|---|---|---|
| 1, 2, 3, 4, 5, 6, 7, 8 | 11 | 0 |
| all 40,320 orders, on average | about 17.92 | 10 |
| 8, 7, 6, 5, 4, 3, 2, 1 | 20 | 16 |
| 7, 5, 3, 1, 8, 6, 4, 2 | 26 | 18 |
| row | Shell sort | insertion sort |
|---|---|---|
| 1,000 numbers in order | 5,457 | 999 |
| 7, 5, 3, 1, 8, 6, 4, 2 | 26 | 22 |
| the same 20 shuffles of 1,000, on average | about 13,991 | about 249,769 |
where the formula comes from
Each count in section 3, in small steps. Only the row in order has a formula; the rest get floors and ceilings.
the move: count one pass on a row in order
the line:8 numbers in order. 4 comparisons with the gap 4, then 7 with the gap 1, and no move. why it is allowed: Each number picked up meets a number no bigger places to its left, so it stays put after one comparison. For 8 numbers and the gap 4, places 5 to 8, 4 comparisons.
the move: list the gaps
the line:why it is allowed: 3 times 1, and 1, makes 4; 3 times 4, and 1, makes 13. So 8 numbers take the gaps 4 and 1.
guess it, thenthe move: add up the passes
the line:why it is allowed: is the comparisons on a row in order, lines 1 and 2 added up. For 8 numbers, 7 + 4, which makes 11.
guess it, thenthe move: no row costs fewer
the line:why it is allowed: On any row each pass picks up the same places, each with a number places to its left, so each is compared at least once.
guess it, thenthe move: so this row is a cheapest one
the line:why it is allowed: By lines 3 and 4. Others tie, as 2, 1, 3, 4, 5, 6, 7, 8 also costs 11.
guess it, thenthe move: a ceiling for each term
the line:why it is allowed: Each is less a gap of at least 1. For 8 numbers, 7 and 4.
guess it, thenthe move: add up the ceilings
the line:why it is allowed: is how many gaps lie below , so by lines 3 and 6, is terms, each less than . For 8 numbers, 11 is at most 2 times 8.
guess it, thenthe move: each gap more than doubles
the line:The gaps 1, 4 and 13 on a line of powers of two, where each step doubles. Each gap sits more than one step right of the one before. why it is allowed: By line 2, three times a gap, and 1, is more than twice it.
guess it, thenthe move: compare the gaps with the powers of 2
the line:gap number 1 1 1 2 4 2 3 13 4 4 40 8 5 121 16 Each gap beside the power of 2 with its number, equal at 1, then ever further above. why it is allowed: Gap number 1 is 1, 2 to the power 0. Each power of 2 is twice the one before, and each gap more than twice, by line 8. So when one gap is at least its power of 2, the next is too, and so on for ever.
guess it, thenthe move: name the largest gap below n
the line:why it is allowed: From 2 numbers on the gap 1 is below , and the gaps grow, by line 8. So the gaps below are gap numbers 1 to .
guess it, thenthe move: use line 9 on the largest gap
the line:why it is allowed: Line 9 with for . For 8 numbers, 4 is at least 2.
guess it, thenthe move: the largest gap is at most n
the line:why it is allowed: By line 10. For 8 numbers, 4 is at most 8.
guess it, thenthe move: put the two together
the line:why it is allowed: By lines 11 and 12.
guess it, thenthe move: read it as a log
the line:why it is allowed: Take the log of both sides of line 13. A bigger number has a bigger log, as the page on halving and logarithms shows, and the log of 2 to a power is that power. For 8 numbers, 1 is at most 3.
guess it, thenthe move: add 1 to both sides
the line:why it is allowed: Line 14 plus 1.
guess it, thenthe move: multiply both sides by n
the line:why it is allowed: Line 15 times , above zero. For 8 numbers, 16 is at most 32.
guess it, thenthe move: put the count of gaps in
the line:why it is allowed: By lines 7 and 16. For 8 numbers, 11 is at most 32.
guess it, thenthe move: a fact for a simpler ceiling
the line:why it is allowed: From 2 numbers on is at least 2, whose log is 1, and a bigger number has a bigger log.
guess it, thenthe move: add the log to both sides
the line:why it is allowed: Line 18 plus .
guess it, thenthe move: add like to like
the line:why it is allowed: Two lots of .
guess it, thenthe move: multiply both sides by n
the line:why it is allowed: Line 20 times , above zero.
guess it, thenthe move: a simpler ceiling
the line:why it is allowed: By lines 17 and 21. For 8 numbers, 11 is at most 48.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 22, under twice , a fixed multiple.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: the count from the first ladder
the line:why it is allowed: The first ladder's line 3. Next, keep only the gaps at most half of , each giving at least half of , and show there are at least of them.
the move: a short gap's pass
the line:why it is allowed: By the first ladder's line 1 it compares times, and is at most half of . For 8 numbers, the gap 4's pass compares 4 times.
guess it, thenthe move: keep only the short gaps' terms
the line:why it is allowed: is how many gaps are at most half of . The other terms are at least 1 each, so dropping them leaves less. For 20 numbers, 19 + 16 + 7 drops to 19 + 16.
guess it, thenthe move: add up the short gaps' floors
the line:why it is allowed: By lines 2 and 3. For 8 numbers, 11 is at least 2 times 4.
guess it, thenthe move: each gap at most four times the one before
the line:The gaps 1, 4 and 13 again. Each sits at most two steps, four times, right of the one before. why it is allowed: By the first ladder's line 2 a gap is three times the one before, plus 1, and that 1 is at most the gap before. 13 is at most 4 times 4.
guess it, thenthe move: compare the gaps with the powers of 4
the line:gap number 1 1 1 2 4 4 3 13 16 4 40 64 5 121 256 Each gap beside the power of 4 with its number, at or below it. why it is allowed: As at the first ladder's line 9, turned round. Gap number 1 is 4 to the power 0. Each power of 4 is four times the one before, and each gap at most four times, by line 5, so each gap stays at or below its power.
guess it, thenthe move: the first gap above half of n
the line:gaps at most gaps below gap number 8 1, 4 1, 4 13 30 1, 4, 13 1, 4, 13 40 60 1, 4, 13 1, 4, 13, 40 40 100 1, 4, 13, 40 1, 4, 13, 40 121 Only at 60 is gap number below and so given a pass. why it is allowed: The gaps grow, by the first ladder's line 8, so the short gaps come first and the next is above half of . For 8 numbers, 13 is above 4.
guess it, thenthe move: so at least half of n
the line:why it is allowed: Above half is at least half.
guess it, thenthe move: use line 6 on the next gap
the line:why it is allowed: Line 6 holds for every gap in the list. For 8 numbers, 13 is at most 4 to the power 2.
guess it, thenthe move: tidy the power
the line:why it is allowed: One less than is .
guess it, thenthe move: put the two together
the line:why it is allowed: By lines 10 and 8. For 8 numbers, 16 is at least 13, which is at least 4.
guess it, thenthe move: write the power of 4 as a power of 2
the line:why it is allowed: Each 4 is 2 × 2. For 8 numbers, 16 is 2 to the power 4.
guess it, thenthe move: double both sides
the line:why it is allowed: Line 12 doubled.
guess it, thenthe move: one more 2
the line:why it is allowed: Times 2 is one more 2 in the power. For 8 numbers, 32 is 2 to the power 5.
guess it, thenthe move: read it as a log
the line:why it is allowed: Take the log of both sides of line 14, as at the first ladder's line 14. For 8 numbers, 5 is at least 3.
guess it, thenthe move: take 1 off both sides
the line:why it is allowed: Line 15 less 1.
guess it, thenthe move: halve both sides
the line:why it is allowed: Line 16 halved. For 8 numbers, 2 is at least 1.
guess it, thenthe move: multiply both sides by half of n
the line:why it is allowed: Line 17 times half of , above zero.
guess it, thenthe move: put the count of short gaps in
the line:why it is allowed: By lines 4 and 18. For 8 numbers, 11 is at least 4.
guess it, thenthe move: multiply the fractions
the line:why it is allowed: A half of a half is a quarter.
guess it, thenthe move: multiply out the bracket
the line:why it is allowed: times each part of the bracket.
guess it, thenthe move: take a quarter of each part
the line:why it is allowed: A quarter of a difference. For 8 numbers, 6 less 2 is 4.
guess it, thenthe move: a fact for the floor
the line:why it is allowed: From 4 numbers on is at least 4, whose log is 2, and a bigger number has a bigger log.
guess it, thenthe move: multiply both sides by an eighth of n
the line:why it is allowed: Line 23 times an eighth of , above zero.
guess it, thenthe move: tidy the left side
the line:why it is allowed: Two eighths are a quarter. For 8 numbers, 2 is at most 3.
guess it, thenthe move: a floor
the line:why it is allowed: Line 22 takes away a quarter of , at most line 25's eighth of , and taking away more leaves less. For 8 numbers, 6 less 3 is 3.
guess it, thenthe move: tidy the floor
the line:why it is allowed: A quarter less an eighth. For 8 numbers, 11 is at least 3.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 27, an eighth being a fixed multiple, from 4 numbers on.
guess it, thenthe move: the ceiling from the first ladder
the line:why it is allowed: The first ladder's line 22.
guess it, thenthe move: a ceiling and a floor together
the line:why it is allowed: Lines 28 and 29, the same growth.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: what in order means
the line:why it is allowed: Each number is at most every number after it.
the move: no number a gap to the left is bigger
the line:why it is allowed: By line 1, so nothing moves, and the row stays in order.
guess it, thenthe move: count the moves
the line:why it is allowed: is the moves on a row in order. A move follows only a comparison finding the left number bigger.
guess it, then
try it with a small : at , line 3 gives , which you can work out by hand
the move: what a move does
the line:why it is allowed: Nothing else moves a number. On 8, 1, 5, 9, 3 the 8 and the 3 trade, 4 places apart.
the move: the pair itself
the line:why it is allowed: Before, the bigger stands first; after, the smaller.
guess it, thenthe move: a number standing between them
the line:is wrong pairs with and before, standing , , after, standing , , smaller than 1 1 equal to 1 0 bigger than , smaller than 2 0 equal to 1 0 bigger than 1 1 stands between the two that trade. Its wrong pairs with them never grow; in the first and last rows the move mends one and makes one. why it is allowed: Before, has on its left and on its right; after, the reverse. A pair is wrong when its bigger number stands first, and equal numbers never are.
guess it, thenthe move: every other pair
the line:why it is allowed: Only and move, and a number outside both stays on the same side of each.
guess it, thenthe move: add it up
the line:8, 1, 5, 9, 3, move by move. Move 1 jumps 4 places and takes the wrong pairs from 5 to 2. Moves 2 and 3 take one each. why it is allowed: The pair mends, by line 2, and lines 3 and 4 add nothing to the count. On 8, 1, 5, 9, 3 only the 5 lies between the 8 and the 3 in size, so 5 wrong pairs become 2.
guess it, thenthe move: the sort ends in order
the line:why it is allowed: The last pass, gap 1 by the first ladder's line 2, is insertion sort. It carries each number left past every bigger one before it, so the front stays in order, one number longer each time. On 7, 31, 42, 12, the 12 passes 42 and 31.
guess it, thenthe move: so the moves are at most the wrong pairs
the line:why it is allowed: By lines 5 and 6. On 8, 1, 5, 9, 3: 3 moves against 5 wrong pairs.
guess it, thenthe move: with the gap 1
the line:why it is allowed: Neighbours have no number between, so by lines 2 and 4 exactly one pair mends.
guess it, thenthe move: insertion sort's moves
the line:why it is allowed: By lines 6 and 8, insertion sort being the gap 1 pass alone. On 8, 1, 5, 9, 3: 5 moves.
guess it, thenthe move: never more than insertion sort
the line:why it is allowed: By lines 7 and 9.
guess it, thenthe move: count the pairs
the line:5, 4, 3, 2, 1, an arc over each wrong pair. All 10 of its pairs are wrong. why it is allowed: Count each pair at its second number, which pairs with each number before it. For 5 numbers, 1 + 2 + 3 + 4, which makes 10.
guess it, thenthe move: no more moves than pairs
the line:why it is allowed: By lines 7 and 11, every wrong pair being a pair.
guess it, thenthe move: average over every shuffle
the line:why it is allowed: is the average moves over every shuffle of different numbers, with no formula known. No shuffle makes more, by line 12, so neither does their average.
guess it, thenthe move: use the page on adding up, its last number here being n - 1
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: Line 13's sum ends at . The box above shows 1 + 2 + ... + 5 is 5 times 6, halved, and the same for any last number. For 5 numbers, 4 times 5, halved, is 10.
guess it, thenthe move: tidy the bracket
the line:why it is allowed: One more than is .
guess it, thenthe move: use the page on adding up, for one row
the line:why it is allowed: Line 12, summed as at line 14.
guess it, thenthe move: tidy the bracket, for one row
the line:why it is allowed: As at line 15. For 5 numbers, at most 10.
guess it, thenthe move: a fact for a simpler ceiling
the line:why it is allowed: Taking 1 away leaves less.
guess it, thenthe move: multiply both sides by n
the line:why it is allowed: Line 18 times , above zero.
guess it, thenthe move: halve both sides
the line:why it is allowed: Line 19 halved. For 5 numbers, 10 is at most 12.5.
guess it, thenthe move: a simpler ceiling
the line:why it is allowed: By lines 15 and 20.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 21, under half of . On this site's 20 shuffles of 1,000 numbers it averaged about 8,968, far under 499,500.
guess it, then
try it on small rows: over every order of 5 numbers, the average is
the move: what a comparison leads to
the line:why it is allowed: A comparison asks if the number places to the left is bigger. If so, a move follows, by the fourth ladder's line 1.
the move: the comparisons followed by no move
the line:why it is allowed: On any row the pass picks up numbers. Each stops after one comparison with no move, or none if no number stands places to its left.
guess it, thenthe move: two kinds of comparison
the line:why it is allowed: By line 1.
guess it, thenthe move: count the first kind
the line:why it is allowed: By the fourth ladder's line 1.
guess it, thenthe move: add up the second kind
the line:why it is allowed: By line 2, summed over the gaps below , which the first ladder's line 3 adds up to .
guess it, thenthe move: put the two kinds together
the line:pass comparisons followed by a move followed by none gap 4 1 3 4 gap 1 8 6 7 in all 9 9 11 42, 7, 31, 18, 56, 23, 12, 49, pass by pass. The comparisons followed by none stay at or under . why it is allowed: By lines 3 to 5. In section 1's run, 9 with a move and 9 without; 9 is at most 11.
guess it, thenthe move: put in the moves' ceiling
the line:why it is allowed: By line 6 above and the fourth ladder's line 17. For 5 numbers, 10 plus 5.
guess it, thenthe move: count the gaps once more
the line:why it is allowed: The gaps grow, by the first ladder's line 8, so the gaps below are different whole numbers from 1 to .
guess it, thenthe move: multiply both sides by n
the line:why it is allowed: Line 8 times , above zero.
guess it, thenthe move: a ceiling for the row in order's count
the line:why it is allowed: By the first ladder's line 7 and line 9 above. Any true ceiling serves. For 5 numbers, 5 is at most 20.
guess it, thenthe move: put it in
the line:why it is allowed: By lines 7 and 10. For 5 numbers, 10 plus 20.
guess it, thenthe move: add the two parts
the line:why it is allowed: is two halves of itself; with the half before, three halves. No row costs more. For 5 numbers, 20 + 10 = 30.
guess it, thenthe move: a floor from the row in order
the line:why it is allowed: is the average comparisons over every shuffle of different numbers, with no formula known. No row costs fewer, by the first ladder's line 4, so neither does the average.
guess it, thenthe move: the second ladder's floor
the line:why it is allowed: By line 13 and the second ladder's line 27. For 5 numbers, the log to base 2 of 5 is about 2.32, an eighth of 5 times that is about 1.45, and all 120 orders average about 7.72.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 14, from 4 numbers on.
guess it, thenthe move: average over every shuffle
the line:why it is allowed: By line 12, an average never being more than its largest. For 5 numbers, about 7.72 is at most 30.
guess it, thenthe move: take three halves of both sides
the line:why it is allowed: The fourth ladder's line 19, times three halves.
guess it, thenthe move: a simpler ceiling
the line:why it is allowed: By lines 16 and 17.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 18, under one and a half times .
guess it, thenthe move: a dearest row is a row
the line:why it is allowed: A dearest row is still a row, so the first ladder's line 4 and line 12 above hold. For 8 numbers, 26, between 11 and 84.
guess it, thenthe move: the second ladder's floor, for a dearest row
the line:why it is allowed: By line 20 and the second ladder's line 27. For 8 numbers, 26 is at least 3.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 21.
guess it, thenthe move: a simpler ceiling, for a dearest row
the line:why it is allowed: By lines 20 and 17. For 8 numbers, 26 is at most 96.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 23. Research finds a tighter ceiling, by an argument beyond this page.
guess it, thenthe move: what is not known
the line:why it is allowed: The floors grow like and the ceilings like , so no tight bound follows. Section 2's slope of about 1.2 is measured, not proved. At 1,000 numbers line 14's floor is about 1,246, line 16's ceiling 1,498,500, and the measured average about 13,991.
guess it, then
try it on small rows: over every order of 5 numbers, the average is about 7.72
count it yourself
Follow every step on 7, 12, 18, 23, 31, 42, 49, 56 and check the comparisons against the first ladder, 7 + 4 = 11. Nothing moves.
| step | the row | what happens | comparisons | moves |
|---|---|---|---|---|
| 1 | 7, 12, 18, 23, 31, 42, 49, 56 | There are 8 numbers in a row. Sort the numbers 4 places apart first, then the neighbours.7, 12, 18, 23, 31, 42, 49, 56 | 0 | 0 |
| 2 | 7, 12, 18, 23, 31, 42, 49, 56 | Compare numbers 4 places apart.7, 12, 18, 23, 31, 42, 49, 56 | 0 | 0 |
| 3 | 7, 12, 18, 23, 31, 42, 49, 56 | Pick up 31 and carry it left in jumps of 4, past every bigger number.7, 12, 18, 23, 31, 42, 49, 56 | 0 | 0 |
| 4 | 7, 12, 18, 23, 31, 42, 49, 56 | 7 is not bigger than 31, so 31 stops here.7, 12, 18, 23, 31, 42, 49, 56 | 1 | 0 |
| 5 | 7, 12, 18, 23, 31, 42, 49, 56 | Pick up 42 and carry it left in jumps of 4, past every bigger number.7, 12, 18, 23, 31, 42, 49, 56 | 1 | 0 |
| 6 | 7, 12, 18, 23, 31, 42, 49, 56 | 12 is not bigger than 42, so 42 stops here.7, 12, 18, 23, 31, 42, 49, 56 | 2 | 0 |
| 7 | 7, 12, 18, 23, 31, 42, 49, 56 | Pick up 49 and carry it left in jumps of 4, past every bigger number.7, 12, 18, 23, 31, 42, 49, 56 | 2 | 0 |
| 8 | 7, 12, 18, 23, 31, 42, 49, 56 | 18 is not bigger than 49, so 49 stops here.7, 12, 18, 23, 31, 42, 49, 56 | 3 | 0 |
| 9 | 7, 12, 18, 23, 31, 42, 49, 56 | Pick up 56 and carry it left in jumps of 4, past every bigger number.7, 12, 18, 23, 31, 42, 49, 56 | 3 | 0 |
| 10 | 7, 12, 18, 23, 31, 42, 49, 56 | 23 is not bigger than 56, so 56 stops here.7, 12, 18, 23, 31, 42, 49, 56 | 4 | 0 |
| 11 | 7, 12, 18, 23, 31, 42, 49, 56 | Now the last pass: compare neighbours, as plain insertion sort does.7, 12, 18, 23, 31, 42, 49, 56 | 4 | 0 |
| 12 | 7, 12, 18, 23, 31, 42, 49, 56 | Pick up 12 and carry it left past every bigger neighbour.7, 12, 18, 23, 31, 42, 49, 56 | 4 | 0 |
| 13 | 7, 12, 18, 23, 31, 42, 49, 56 | 7 is not bigger than 12, so 12 stops here.7, 12, 18, 23, 31, 42, 49, 56 | 5 | 0 |
| 14 | 7, 12, 18, 23, 31, 42, 49, 56 | Pick up 18 and carry it left past every bigger neighbour.7, 12, 18, 23, 31, 42, 49, 56 | 5 | 0 |
| 15 | 7, 12, 18, 23, 31, 42, 49, 56 | 12 is not bigger than 18, so 18 stops here.7, 12, 18, 23, 31, 42, 49, 56 | 6 | 0 |
| 16 | 7, 12, 18, 23, 31, 42, 49, 56 | Pick up 23 and carry it left past every bigger neighbour.7, 12, 18, 23, 31, 42, 49, 56 | 6 | 0 |
| 17 | 7, 12, 18, 23, 31, 42, 49, 56 | 18 is not bigger than 23, so 23 stops here.7, 12, 18, 23, 31, 42, 49, 56 | 7 | 0 |
| 18 | 7, 12, 18, 23, 31, 42, 49, 56 | Pick up 31 and carry it left past every bigger neighbour.7, 12, 18, 23, 31, 42, 49, 56 | 7 | 0 |
| 19 | 7, 12, 18, 23, 31, 42, 49, 56 | 23 is not bigger than 31, so 31 stops here.7, 12, 18, 23, 31, 42, 49, 56 | 8 | 0 |
| 20 | 7, 12, 18, 23, 31, 42, 49, 56 | Pick up 42 and carry it left past every bigger neighbour.7, 12, 18, 23, 31, 42, 49, 56 | 8 | 0 |
| 21 | 7, 12, 18, 23, 31, 42, 49, 56 | 31 is not bigger than 42, so 42 stops here.7, 12, 18, 23, 31, 42, 49, 56 | 9 | 0 |
| 22 | 7, 12, 18, 23, 31, 42, 49, 56 | Pick up 49 and carry it left past every bigger neighbour.7, 12, 18, 23, 31, 42, 49, 56 | 9 | 0 |
| 23 | 7, 12, 18, 23, 31, 42, 49, 56 | 42 is not bigger than 49, so 49 stops here.7, 12, 18, 23, 31, 42, 49, 56 | 10 | 0 |
| 24 | 7, 12, 18, 23, 31, 42, 49, 56 | Pick up 56 and carry it left past every bigger neighbour.7, 12, 18, 23, 31, 42, 49, 56 | 10 | 0 |
| 25 | 7, 12, 18, 23, 31, 42, 49, 56 | 49 is not bigger than 56, so 56 stops here.7, 12, 18, 23, 31, 42, 49, 56 | 11 | 0 |
| 26 | 7, 12, 18, 23, 31, 42, 49, 56 | In order, after 11 comparisons and no moves.7, 12, 18, 23, 31, 42, 49, 56 | 11 | 0 |
In all, 11 comparisons. The formula from section 4, , gives at : the same.
where you meet it
Shell sort is short and needs no room beyond the row. Where you meet it, and what is used instead.
inside the bzip2 compressor
bzip2 makes files smaller. For each block of a file it sorts every reading of the block that starts at one of its places and wraps round. On a block of 10,000 places or more it groups the readings by their first letters. A group that is short, or still alike after many letters, it sorts with Shell sort and these gaps. Its table holds fourteen, 1, 4, 13, 40, 121, 364, 1,093, 3,280, 9,841, 29,524, 88,573, 265,720, 797,161 and 2,391,484.
source: bzip2's code, blocksort.c (mainSimpleSort, BZ2_blockSort and the table of gaps), read from libbz2.so.1.0.4 as installed
a lesson in what long jumps are worth
On the same 20 shuffles of 10,000 numbers, Shell sort averaged about 235,647 comparisons against insertion sort's about 25,045,660, about a hundredth.
source: this site's measured runs of both sorts, 20 shuffles at each length
what is used instead
The ready-made sorts of Python, Java and C++ use no Shell sort, and each hands short stretches to a form of insertion sort.
how it compares
Shell sort beside the other ways to put a row in order. Its line on the plot is points only, as no formula for its average is known.
- 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.