Sorting algorithms
SOF ICSO · AI, Robotics and IT · about 7 minutes
An algorithm is a precise list of steps. Bubble sort compares neighbours and swaps them when they are out of order, again and again, until a full pass makes no swaps.
Watch the bars swap and the biggest bubble to the end.
Bubble sort
Bubble sort compares neighbours and swaps them if they are in the wrong order. After the first pass the largest value has ‘bubbled’ to the end. A list of n items needs at most n − 1 passes.
In the worst case it makes n(n − 1)/2 comparisons, so it is slow for big lists.
Selection and insertion sort
Selection sort finds the smallest item and swaps it to the front, then repeats on the rest. Insertion sort takes each new item and slides it back into its place among the sorted ones, like sorting playing cards in your hand.
Insertion sort is fast when the list is almost sorted already.
Searching a sorted list
Binary search looks at the middle item and throws away half the list each time. A list of 1000 items needs at most 10 looks, because 2¹⁰ = 1024.
Linear search checks every item and may need all n looks, but works on unsorted lists.
Worked examples
Example 1Show the list 5, 1, 4, 2 after one pass of bubble sort.
- 5, 1 → swap: 1, 5, 4, 2.
- 5, 4 → swap: 1, 4, 5, 2.
- 5, 2 → swap: 1, 4, 2, 5.
Answer: 1, 4, 2, 5
Example 2At most how many comparisons does binary search need for 100 sorted items?
- 2⁶ = 64 < 100 ≤ 128 = 2⁷.
- So 7 halvings are enough.
Answer: 7
Where marks are lost
- Doing a full sort when the question asks for the list after one pass.
- Using binary search on an unsorted list.
- Counting passes instead of comparisons.
Exam tips
- After the first pass, the largest item is in its final place.
- Sorting n items with bubble sort can take about n² comparisons.
- Searching a sorted list is much faster (binary search).
Practice questions
After one bubble-sort pass on [4, 2, 5, 1], the list is…
Binary search on a sorted list of 16 items needs at most about how many checks?
A step-by-step method to solve a problem is called…
More worked examples
Try each one first, then open the working.
How many swaps happen during the first pass?
Show the working
Start: Go pair by pair, counting each swap.
After one pass: [29, 10, 37, 31, 12, 40] with 4 swaps.
Answer: 4
Binary search looks for 9, using mid = (low + high) // 2. How many items are compared before it is found?
Show the working
Start: Start with low = 0, high = 14.
3 comparisons: the list is halved each time.
Answer: 3
Bubble sort without the early-exit check sorts 6 items. How many comparisons does it make?
Show the working
Start: Each pass compares one fewer pair.
(n − 1) + (n − 2) + … + 1 = n(n − 1)/2 = 15.
Answer: 15
After the first full pass of bubble sort (ascending), what number is at index 2?
Show the working
Start: Compare neighbours left to right and swap if the left one is bigger.
After one pass: [9, 14, 21, 8, 14, 31].
Answer: 21
Practise more
New questions every time, at exam level
How many swaps happen during the first pass?