A recipe
Sorting a row of cards
Draft: written by an AI helper and not yet checked by a teacher.
Tap a paragraph to see it in words.
Ingredients: five cards numbered one to five, face up in a row in the order four, two, five, one, three; a table; patience.
Ingredients: cards in the row .
.
Method: start with the second card. Compare it with the card on its left. If that one is bigger, swap them, and keep swapping until the card on the left is smaller or you reach the start. Then do the same for the third card, the fourth and the fifth.
Method: for , compare card with its left neighbour and swap while the left one is bigger.
: while , swap; counts the swaps.
In this row I made six swaps in all, and the row ended as one, two, three, four, five. Six is exactly the number of pairs that were in the wrong order: four before two, four before one, four before three, two before one, five before one and five before three.
Here: swaps, and the row becomes . The pairs in the wrong order are , that is of them, one swap for each.
, , .
Serving suggestion: a row in exactly the opposite order needs the most swaps. Every pair is wrong, which is five times four over two, ten swaps. Ten cards in reverse need forty-five. Twice the cards, more than four times the work.
Worst case: a reversed row, all pairs wrong, so swaps. For it is . Doubling multiplies the work by more than .
Worst: ; ; .
Letters of words: 663 → 254 → 28.
How to read the signs
- x i: the card in place i
- the number of pairs in I: the pairs in the wrong order
- n choose two: the number of pairs among n
- so; it follows that
- such that
- is bigger than