pairs out of order (inversions)
counting the cost
what it is
A pair out of order, an inversion, is two numbers anywhere in a row with the bigger one first. A method that sorts by swapping only neighbours out of order makes one swap per inversion. That is at most on different numbers, and on average over every order. On a row back to front, such as 4, 3, 2, 1, no method that swaps only neighbours makes fewer than .
Take the row 4, 1, 3, 2. Rows here never hold a number twice. Its 4 numbers make 6 pairs. In 4 of them the bigger number comes first, such as 4 before 1: each is out of order, an inversion. A row in order, smallest first, has none. Here is how many numbers the row holds.
| the pair | out of order |
|---|---|
| 4 before 1 | yes |
| 4 before 3 | yes |
| 4 before 2 | yes |
| 1 before 3 | |
| 1 before 2 | |
| 3 before 2 | yes |
| all 6 pairs | 4 |
Neighbours are two numbers side by side. Bubble sort, cocktail shaker sort and insertion sort each sort a row by swapping neighbours out of order; insertion sort calls such a swap a move. Below, bubble sort takes 4, 1, 3, 2 from 4 inversions to none in 4 swaps.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: two other numbers
the line:why it is allowed: Call the two neighbours swapped the moving pair. Only they change places, so two other numbers keep their order. Swapping 4 and 1 in 4, 1, 3, 2 leaves 3 before 2.
the move: one other number
the line:the pair out of order in 4, 1, 3, 2 out of order in 1, 4, 3, 2 4 and 1 yes 4 and 3 yes yes 4 and 2 yes yes 1 and 3 1 and 2 3 and 2 yes yes inversions 4 3 Swapping the neighbours 4 and 1 changes only their own pair, so 4 inversions become 3. why it is allowed: No number stands between the moving pair, so any other number stands before both or after both, before the swap and after it, and keeps its order with each. Here 3 and 2 stand after both 4 and 1.
guess it, thenthe move: the moving pair
the line:why it is allowed: Before the swap the bigger of the two comes first; after it the smaller does.
guess it, thenthe move: add up the changes
the line:why it is allowed: By lines 1 and 2 every other pair stays as it was, in order or out of order. By line 3 the moving pair stops being an inversion.
guess it, thenthe move: name the drop
the line:why it is allowed: Let be how many inversions the swap takes away. By line 4 it is 1.
guess it, thenthe move: read it
the line:why it is allowed: By line 5, whatever the row and whichever neighbours out of order are swapped.
guess it, then
try it with a small : at , line 5 gives , which you can work out by hand
the move: never stuck
the line:why it is allowed: Walk along the row from the bigger number of a pair out of order to the smaller. Starting high and ending low, the walk steps down somewhere: two neighbours out of order. In 1, 3, 4, 2, from 3 to 2 it steps down at 4, 2.
the move: a swap per inversion
the line:Insertion sort on 3, 4, 2, 1. Each move mends one arc, 5 in all. row inversions bubble sort's swaps cocktail shaker sort's swaps insertion sort's moves 4, 1, 3, 2 4 4 4 4 3, 4, 2, 1 5 5 5 5 On each row, every sort's count is the row's inversions. why it is allowed: By question 1 each swap takes off one inversion, and by line 1 the method goes on until none is left. So it makes one swap per inversion the row starts with, in whatever order it swaps.
guess it, thenthe move: back to front
the line:4, 3, 2, 1 with an arc over every one of its 6 pairs. why it is allowed: Each number of a row back to front is smaller than the one before it, so bigger than every number after it.
guess it, thenthe move: no row has more
the line:why it is allowed: No row has more inversions than pairs, and by line 3 a row back to front has every pair out of order.
guess it, thenthe move: any other row
the line:why it is allowed: Any other row has a number bigger than the one before it: two neighbours in order, a pair that is no inversion. So it has fewer inversions than pairs. In 4, 3, 1, 2 the neighbours 1 and 2 are in order: 5 inversions, not 6.
guess it, thenthe move: the most swaps, all pairs
the line:why it is allowed: By line 2 a row's swaps are its inversions, and by lines 3 to 5 the most, on a row back to front, are all the pairs.
guess it, thenthe move: count the pairs
the line:why it is allowed: The first number pairs with the after it, the second with one fewer, and so on, to the second last with the last. For 4 numbers, 3 + 2 + 1 makes 6 pairs.
guess it, thenthe move: name the most swaps
the line:why it is allowed: Let be the most swaps on a row of numbers. By lines 6 and 7 it is .
guess it, thenthe move: add the other way round
the line:why it is allowed: The same numbers in the other order give the same total.
guess it, thenthe move: use adding up, its n as n - 1
the line:this page's the sum that page's the total 3 1 + 2 2 3 4 1 + 2 + 3 3 6 That page's is one less than this page's. 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: The page on adding up adds whole numbers. Those from 1 to any number add up to that number times one more than it, halved: 1 + 2 + 3 is 3 times 4, halved, which is 6. It calls that last number ; read it as this page's . On 4 numbers the sum is 1 + 2 + 3, and that page's is 3.
guess it, thenthe move: tidy the bracket
the line:why it is allowed: is .
guess it, thenthe move: read it
the line:why it is allowed: By lines 2 and 4 a row back to front takes the most swaps, and by line 11 that is : 6 on 4 numbers. Bubble sort, cocktail shaker sort and insertion sort are such methods, so each makes that many.
guess it, then
try it with a small : at , line 8 gives , which you can work out by hand
the move: other pairs stay
the line:why it is allowed: Swap two neighbours in order, the smaller first. Lines 1 and 2 of question 1 hold here too, as they never use which is bigger. Swapping 1 and 3 in 4, 1, 3, 2 leaves 4 before both and 2 after both.
the move: the moving pair turns
the line:why it is allowed: Before the swap the smaller of the two comes first; after it the bigger does.
guess it, thenthe move: add up the changes
the line:Every arc still joins its two numbers; the new one, 3 before 1, is red. 4 inversions become 5. why it is allowed: By lines 1 and 2. In 4, 1, 3, 2, swapping 1 and 3 gives 4, 3, 1, 2.
guess it, thenthe move: any swap, by exactly 1
the line:why it is allowed: By question 1 a swap of neighbours out of order takes off 1, and by line 3 one in order adds 1. So none takes off more than 1.
guess it, thenthe move: a swap per inversion, at least
the line:why it is allowed: The row ends in order, with no inversion, and by line 4 each swap takes off at most one. So it takes a swap per inversion, at least.
guess it, thenthe move: back to front
the line:why it is allowed: By question 2 a row back to front has inversions, so by line 5 it takes at least that many swaps.
guess it, thenthe move: that many is enough
the line:why it is allowed: By question 2, bubble sort, one such method, makes exactly that many on a row back to front.
guess it, thenthe move: at least, and reached
the line:why it is allowed: By line 6 no such method makes fewer, and by line 7 one makes exactly that many.
guess it, thenthe move: name the fewest swaps
the line:why it is allowed: Let be the fewest swaps any such method makes on numbers back to front: by line 8, . Below, is 2 or more, as line 11 explains.
guess it, thenthe move: write it as a product
the line:- the fewest swaps,
- a quarter of ,
The fewest swaps back to front beside a quarter of , for 1 to 10 numbers. The quarter is above only at 1; they meet at 2. why it is allowed: Halving a product halves one of its parts. The lines below show never drops below a quarter of , read squared. For 4, is 3 times 2, which is 6. 4 squared is 16, and a quarter of it is 4.
guess it, thenthe move: half of n, at least 1
the line:why it is allowed: Read as is at least. is at least 2; halve both sides. For 4, 2 is at least 1. At 1 it fails, half of 1 being under 1.
guess it, thenthe move: add half of n
the line:why it is allowed: Add half of to both sides of line 11. On the left, two halves of make . For 4, 4 is at least 2 + 1.
guess it, thenthe move: take 1 away
the line:why it is allowed: Take 1 from both sides of line 12. For 4, 3 is at least 2.
guess it, thenthe move: put n/2 for n - 1
the line:why it is allowed: By line 13, is at least . Multiply both sides by , a number above zero, so at least still holds. The left side is then , by line 10. For 4, 6 is at least 2 times 2, which is 4.
guess it, thenthe move: multiply out
the line:a quarter of the fewest swaps back to front 2 1 1 3 2.25 3 4 4 6 10 25 45 From 2 numbers on, the fewest swaps never drop below a quarter of . why it is allowed: Half of times half of is a quarter of times .
guess it, thenthe move: read it as a floor
the line:why it is allowed: By line 15, from 2 numbers on, never drops below a quarter of , a fixed multiple of above zero: a floor. , read is in big omega of , says so, as the page 'reading , and ' explains.
guess it, then
try it with a small : at , line 9 gives , which you can work out by hand
the move: swaps are inversions
the line:why it is allowed: By question 2 the method makes one swap per inversion on every row. So its average swaps over every shuffle, the expected number, equal the average inversions. 1, 2 and 3 stand in 6 orders, all in line 2's drawing.
the move: a row and its reverse
the line:Each shuffle of 1, 2 and 3 beside its reverse. Each pair of numbers is out of order in one of the two, so 3 arcs a couple, 9 in all. why it is allowed: Reversing a row reads it from its end: 1, 3, 2 gives 2, 3, 1. Each pair of numbers trades which comes first. So each pair is out of order in exactly one of the two, and their inversions add up to the pairs, 3 here.
guess it, thenthe move: couple up the shuffles
the line:why it is allowed: No row of 2 or more different numbers is its own reverse: the reverse starts with the row's last number, not its first. Reversing twice gives the row back. So the shuffles pair off: the six of 1, 2 and 3 make three couples.
guess it, thenthe move: add couple by couple
the line:why it is allowed: By line 2 each couple holds as many inversions as pairs, and by line 3 the couples are half the shuffles. For 3 numbers, 3 pairs times 3 couples is 9.
guess it, thenthe move: divide by the shuffles
the line:why it is allowed: The average is line 4's total over the number of shuffles, which leaves half the pairs. For 3 numbers, 9 over 6 shuffles is 1.5, half of 3.
guess it, thenthe move: count the pairs
the line:why it is allowed: As line 7 of question 2 counts them. For 3 numbers, 2 + 1 makes 3 pairs.
guess it, thenthe move: name the average
the line:why it is allowed: Let be the average inversions over every shuffle of different numbers, every order as likely, so by line 1 the average swaps. By lines 5 and 6 it is half of .
guess it, thenthe move: put in question 2's sum
the line:why it is allowed: By lines 8 to 11 of question 2, is . For 3 numbers, 2 + 1 is 3, and 2 times 3 over 2 is 3 too.
guess it, thenthe move: multiply out
the line:why it is allowed: Half of a number over 2 is that number over 4. For 3 numbers, 2 times 3 over 4 is 1.5, as line 5 found.
guess it, thenthe move: read it
the line:why it is allowed: By line 9 and question 2, a shuffle takes, on average, half the swaps of a row back to front: 1.5 against 3 on 3 numbers. The page 'expected cost' finds this in its question 2, each pair being out of order with chance one half.
guess it, then
try it on small rows: over every order of 3 numbers, the average is
a worked example
Count the inversions of 5, 2, 4, 1, 3, and a sort's swaps on it.
- 5 comes before four smaller numbers, 2 before one smaller, 4 before two smaller, and 1 and 3 before none: 4 + 1 + 2 makes 7 inversions.
- By question 2, bubble sort, cocktail shaker sort and insertion sort each make 7 swaps on it.
- Against that, 5 numbers back to front take 10, by question 2, and a shuffle of 5 takes 5 on average, by question 4.
the trap
check yourself
How many inversions has the row 2, 3, 1?
the answer
Two, 2 before 1 and 3 before 1; 2 before 3 is in order.
How many swaps does bubble sort make on 10 numbers back to front?
the answer
By question 2, 9 times 10 over 2, which is 45.
On average, how many inversions has a shuffle of 8 numbers?
the answer
By question 4, 7 times 8 over 4, which is 14, half of the 28 pairs.