Linear search checks every item; binary search halves a sorted list. Bubble sort and selection sort put a list in order pass by pass.
| Linear search checks | the position of the item, or all n if it is not there |
| Binary search, worst case for n items | the smallest k with 2ᵏ > n: 100 items → 7, 1,000 → 10 |
| Bubble pass on 5, 1, 4, 2 | 1, 4, 2, 5 (3 swaps) |
| Total bubble swaps | the number of pairs that are out of order |
| Selection sort, n items | n − 1 passes |
Bubble sort swaps neighbouring items that are in the wrong order. How many swaps in all does it make to sort 52, 28, 36, 43, 3, 54?
Working: every swap fixes one out-of-order pair; count those pairs
Answer: 7