Skip to content

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 (n−1)n2\frac{(n - 1)n}{2} on nn different numbers, and (n−1)n4\frac{(n - 1)n}{4} 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 (n−1)n2\frac{(n - 1)n}{2}.

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 nn is how many numbers the row holds.

4132the row4 wrong pairsa wrong pair, bigger before smaller
4, 1, 3, 2 with an arc over each pair out of order, 4 in all. The drawing calls them wrong pairs.
the pairout of order
4 before 1yes
4 before 3yes
4 before 2yes
1 before 3
1 before 2
3 before 2yes
all 6 pairs4
Every pair of 4, 1, 3, 2 as it stands.

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.

4132start4 wrong pairs1432swap 13 wrong pairs1342swap 22 wrong pairs1324swap 31 wrong pair1234swap 4no wrong pairsa wrong pair, bigger before smallerjust swappedthe pair it mended
Bubble sort on 4, 1, 3, 2. Each swap mends one arc, dashed, and leaves the rest. The inversions go 4, 3, 2, 1, 0.

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 nn, 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.

question 1Swap two neighbours that are out of order. How do the inversions change?
  1. the move: two other numbers

    the line:
    a pair of two other numberskeeps its order\begin{gathered} \text{a pair of two other numbers} \\ \text{keeps its order} \end{gathered}

    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.

  2. the move: one other number

    the line:
    a pair with one of the twokeeps its order\begin{gathered} \text{a pair with one of the two} \\ \text{keeps its order} \end{gathered}
    the pairout of order in 4, 1, 3, 2out of order in 1, 4, 3, 2
    4 and 1yes
    4 and 3yesyes
    4 and 2yesyes
    1 and 3
    1 and 2
    3 and 2yesyes
    inversions43
    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.

  3. the move: the moving pair

    the line:
    the moving pair goes fromout of order to in order\begin{gathered} \text{the moving pair goes from} \\ \text{out of order to in order} \end{gathered}

    why it is allowed: Before the swap the bigger of the two comes first; after it the smaller does.

  4. the move: add up the changes

    the line:
    the swap takes away oneinversion and adds none\begin{gathered} \text{the swap takes away one} \\ \text{inversion and adds none} \end{gathered}

    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.

  5. the move: name the drop

    the line:
    D=1D = 1

    why it is allowed: Let DD be how many inversions the swap takes away. By line 4 it is 1.

  6. the move: read it

    the line:
    one swap of neighboursout of order takes offexactly one inversion\begin{gathered} \text{one swap of neighbours} \\ \text{out of order takes off} \\ \text{exactly one inversion} \end{gathered}

    why it is allowed: By line 5, whatever the row and whichever neighbours out of order are swapped.

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

question 2A method swaps only neighbours out of order until the row is in order. At most how many swaps does it make?
  1. the move: never stuck

    the line:
    while some pair is out of order,some two neighbours are\begin{gathered} \text{while some pair is out of order,} \\ \text{some two neighbours are} \end{gathered}
    1342

    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.

  2. the move: a swap per inversion

    the line:
    one swap for each inversionthe row starts with\begin{gathered} \text{one swap for each inversion} \\ \text{the row starts with} \end{gathered}
    3421start5 wrong pairs3241move 14 wrong pairs2341move 23 wrong pairs2314move 32 wrong pairs2134move 41 wrong pair1234move 5no wrong pairsa wrong pair, bigger before smallerjust swappedthe pair it mended
    Insertion sort on 3, 4, 2, 1. Each move mends one arc, 5 in all.
    rowinversionsbubble sort's swapscocktail shaker sort's swapsinsertion sort's moves
    4, 1, 3, 24444
    3, 4, 2, 15555
    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.

  3. the move: back to front

    the line:
    a row back to front hasevery pair out of order\begin{gathered} \text{a row back to front has} \\ \text{every pair out of order} \end{gathered}
    4321back to front6 wrong pairsa wrong pair, bigger before smaller
    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.

  4. the move: no row has more

    the line:
    no row has more inversionsthan a row back to front\begin{gathered} \text{no row has more inversions} \\ \text{than a row back to front} \end{gathered}

    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.

  5. the move: any other row

    the line:
    any other row hasfewer inversions\begin{gathered} \text{any other row has} \\ \text{fewer inversions} \end{gathered}

    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.

  6. the move: the most swaps, all pairs

    the line:
    the most swaps equalthe number of pairs\begin{gathered} \text{the most swaps equal} \\ \text{the number of pairs} \end{gathered}

    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.

  7. the move: count the pairs

    the line:
    n numbers make(n−1)+⋯+2+1 pairs\begin{gathered} n \text{ numbers make} \\ (n - 1) + \dots + 2 + 1 \text{ pairs} \end{gathered}

    why it is allowed: The first number pairs with the n−1n - 1 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.

  8. the move: name the most swaps

    the line:
    W=(n−1)+⋯+2+1W = (n - 1) + \dots + 2 + 1

    why it is allowed: Let WW be the most swaps on a row of nn numbers. By lines 6 and 7 it is (n−1)+⋯+2+1(n - 1) + \dots + 2 + 1.

  9. the move: add the other way round

    the line:
    W=1+2+⋯+(n−1)W = 1 + 2 + \dots + (n - 1)

    why it is allowed: The same numbers in the other order give the same total.

  10. the move: use adding up, its n as n - 1

    the line:
    W=(n−1)((n−1)+1)2W = \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2}
    this page's nnthe sumthat page's nnthe total
    31 + 223
    41 + 2 + 336
    That page's nn is one less than this page's.
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      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 nn 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 n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    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 nn; read it as this page's n−1n - 1. On 4 numbers the sum is 1 + 2 + 3, and that page's nn is 3.

  11. the move: tidy the bracket

    the line:
    W=(n−1)n2W = \frac{(n - 1)n}{2}

    why it is allowed: (n−1)+1(n - 1) + 1 is nn.

  12. the move: read it

    the line:
    a row back to fronttakes the most,(n−1)n2 swaps\begin{gathered} \text{a row back to front} \\ \text{takes the most,} \\ \frac{(n - 1)n}{2} \text{ swaps} \end{gathered}

    why it is allowed: By lines 2 and 4 a row back to front takes the most swaps, and by line 11 that is (n−1)n2\frac{(n - 1)n}{2}: 6 on 4 numbers. Bubble sort, cocktail shaker sort and insertion sort are such methods, so each makes that many.

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

question 3A method swaps only neighbours, out of order or not, until the row is in order. At least how many swaps does it make on a row back to front?
  1. the move: other pairs stay

    the line:
    swapping neighbours in order,every other pair keeps its order\begin{gathered} \text{swapping neighbours in order,} \\ \text{every other pair keeps its order} \end{gathered}

    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.

  2. the move: the moving pair turns

    the line:
    the moving pair goes fromin order to out of order\begin{gathered} \text{the moving pair goes from} \\ \text{in order to out of order} \end{gathered}

    why it is allowed: Before the swap the smaller of the two comes first; after it the bigger does.

  3. the move: add up the changes

    the line:
    a swap of neighbours in orderadds exactly one inversion\begin{gathered} \text{a swap of neighbours in order} \\ \text{adds exactly one inversion} \end{gathered}
    arcs4132443125
    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.

  4. the move: any swap, by exactly 1

    the line:
    any swap of neighbours takes offor adds exactly one inversion\begin{gathered} \text{any swap of neighbours takes off} \\ \text{or adds exactly one inversion} \end{gathered}

    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.

  5. the move: a swap per inversion, at least

    the line:
    at least as many swaps asthe row has inversions\begin{gathered} \text{at least as many swaps as} \\ \text{the row has inversions} \end{gathered}

    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.

  6. the move: back to front

    the line:
    back to front, at least(n−1)n2 swaps\begin{gathered} \text{back to front, at least} \\ \frac{(n - 1)n}{2} \text{ swaps} \end{gathered}

    why it is allowed: By question 2 a row back to front has (n−1)n2\frac{(n - 1)n}{2} inversions, so by line 5 it takes at least that many swaps.

  7. the move: that many is enough

    the line:
    bubble sort makesexactly that many\begin{gathered} \text{bubble sort makes} \\ \text{exactly that many} \end{gathered}

    why it is allowed: By question 2, bubble sort, one such method, makes exactly that many on a row back to front.

  8. the move: at least, and reached

    the line:
    the fewest swaps back to frontare exactly (n−1)n2\begin{gathered} \text{the fewest swaps back to front} \\ \text{are exactly } \frac{(n - 1)n}{2} \end{gathered}

    why it is allowed: By line 6 no such method makes fewer, and by line 7 one makes exactly that many.

  9. the move: name the fewest swaps

    the line:
    L=(n−1)n2L = \frac{(n - 1)n}{2}

    why it is allowed: Let LL be the fewest swaps any such method makes on nn numbers back to front: by line 8, (n−1)n2\frac{(n - 1)n}{2}. Below, nn is 2 or more, as line 11 explains.

  10. the move: write it as a product

    the line:
    L=(n−1)×n2L = (n - 1) \times \frac{n}{2}
    swapsnumbers in the row010203040500246810swapsnumbers in the row010203040500246810
    • the fewest swaps, (n−1)n2\frac{(n - 1)n}{2}
    • a quarter of n2n^2, n24\frac{n^2}{4}
    The fewest swaps back to front beside a quarter of n2n^2, 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 LL never drops below a quarter of n2n^2, read nn squared. For 4, LL is 3 times 2, which is 6. 4 squared is 16, and a quarter of it is 4.

  11. the move: half of n, at least 1

    the line:
    n2≥1\frac{n}{2} \ge 1

    why it is allowed: Read ≥\ge as is at least. nn is at least 2; halve both sides. For 4, 2 is at least 1. At 1 it fails, half of 1 being under 1.

  12. the move: add half of n

    the line:
    n≥n2+1n \ge \frac{n}{2} + 1

    why it is allowed: Add half of nn to both sides of line 11. On the left, two halves of nn make nn. For 4, 4 is at least 2 + 1.

  13. the move: take 1 away

    the line:
    n−1≥n2n - 1 \ge \frac{n}{2}

    why it is allowed: Take 1 from both sides of line 12. For 4, 3 is at least 2.

  14. the move: put n/2 for n - 1

    the line:
    L≥n2×n2L \ge \frac{n}{2} \times \frac{n}{2}

    why it is allowed: By line 13, n−1n - 1 is at least n2\frac{n}{2}. Multiply both sides by n2\frac{n}{2}, a number above zero, so at least still holds. The left side is then LL, by line 10. For 4, 6 is at least 2 times 2, which is 4.

  15. the move: multiply out

    the line:
    L≥n24L \ge \frac{n^2}{4}
    nna quarter of n2n^2the fewest swaps back to front
    211
    32.253
    446
    102545
    From 2 numbers on, the fewest swaps never drop below a quarter of n2n^2.

    why it is allowed: Half of nn times half of nn is a quarter of nn times nn.

  16. the move: read it as a floor

    the line:
    L∈Ω(n2)L \in \Omega(n^2)

    why it is allowed: By line 15, from 2 numbers on, LL never drops below a quarter of n2n^2, a fixed multiple of n2n^2 above zero: a floor. L∈Ω(n2)L \in \Omega(n^2), read LL is in big omega of n2n^2, says so, as the page 'reading OO, Ω\Omega and Θ\Theta' explains.

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

question 4A method swaps only neighbours out of order until a shuffle of nn different numbers is in order. How many swaps on average?
  1. the move: swaps are inversions

    the line:
    each shuffle takes one swapfor each inversion it starts with\begin{gathered} \text{each shuffle takes one swap} \\ \text{for each inversion it starts with} \end{gathered}

    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.

  2. the move: a row and its reverse

    the line:
    between a row and its reverse,every pair is out of orderin exactly one\begin{gathered} \text{between a row and its reverse,} \\ \text{every pair is out of order} \\ \text{in exactly one} \end{gathered}
    arcsarcs1230132121313213231231229 arcs in 6 shuffles
    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.

  3. the move: couple up the shuffles

    the line:
    the shuffles fall into couples,each a row and its reverse\begin{gathered} \text{the shuffles fall into couples,} \\ \text{each a row and its reverse} \end{gathered}

    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.

  4. the move: add couple by couple

    the line:
    all the shuffles together holdthe pairs times half the shuffles\begin{gathered} \text{all the shuffles together hold} \\ \text{the pairs times half the shuffles} \end{gathered}

    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.

  5. the move: divide by the shuffles

    the line:
    the average is halfthe number of pairs\begin{gathered} \text{the average is half} \\ \text{the number of pairs} \end{gathered}

    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.

  6. the move: count the pairs

    the line:
    n numbers make(n−1)+⋯+2+1 pairs\begin{gathered} n \text{ numbers make} \\ (n - 1) + \dots + 2 + 1 \text{ pairs} \end{gathered}

    why it is allowed: As line 7 of question 2 counts them. For 3 numbers, 2 + 1 makes 3 pairs.

  7. the move: name the average

    the line:
    V=12×((n−1)+⋯+2+1)V = \frac{1}{2} \times \bigl((n - 1) + \dots + 2 + 1\bigr)

    why it is allowed: Let VV be the average inversions over every shuffle of nn different numbers, every order as likely, so by line 1 the average swaps. By lines 5 and 6 it is half of (n−1)+⋯+2+1(n - 1) + \dots + 2 + 1.

  8. the move: put in question 2's sum

    the line:
    V=12×(n−1)n2V = \frac{1}{2} \times \frac{(n - 1)n}{2}

    why it is allowed: By lines 8 to 11 of question 2, (n−1)+⋯+2+1(n - 1) + \dots + 2 + 1 is (n−1)n2\frac{(n - 1)n}{2}. For 3 numbers, 2 + 1 is 3, and 2 times 3 over 2 is 3 too.

  9. the move: multiply out

    the line:
    V=(n−1)n4V = \frac{(n - 1)n}{4}

    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.

  10. the move: read it

    the line:
    half the most,on average\begin{gathered} \text{half the most,} \\ \text{on average} \end{gathered}

    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.

try it on small rows: over every order of 3 numbers, the average is V=1.5V = 1.5

a worked example

Count the inversions of 5, 2, 4, 1, 3, and a sort's swaps on it.

  1. 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.
  2. By question 2, bubble sort, cocktail shaker sort and insertion sort each make 7 swaps on it.
  3. 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

  1. 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.

  2. 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.

  3. 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.