Bubble Sort, Shuttle Sort and Quick Sort Step by Step | OCR B (MEI) A Level Further Maths notes
Free preview: Bubble Sort, Shuttle Sort and Quick Sort Step by Step
Bubble Sort: Let the Biggest Rise
Year 1 (AS) for OCR B (MEI). For Edexcel and OCR A, check your own specification for which parts are AS and which are A level.
Let's start with why sorting matters. Try finding "Patel" in a phone list of 10 000 names in random order: you might check every single name. If the list is in alphabetical order you can jump straight to the P's. Many later algorithms (bin packing, Kruskal's algorithm) also begin by sorting. So we need reliable ways to sort.
The words we need
- A sorting algorithm puts a list into ascending (smallest first) or descending (largest first) order.
- A comparison is looking at two items to see which comes first. A swap is exchanging them.
- A pass is one run through the list (or the part of it still being sorted).
- We use the list 23, 15, 31, 19, 8, 27, 12 for all three sorts, in ascending order, so you can compare them fairly.
Bubble sort: the recipe
- Compare the first two items. If they are in the wrong order, swap them. Why: we only ever fix neighbours, which is easy to do by hand.
- Move along one place and compare the 2nd and 3rd items, then the 3rd and 4th, and so on to the end of the list. That is one pass. Why: the larger of each pair keeps moving right.
- After the pass, the last item is in its final place. Leave it alone from now on. Why: the largest item wins every comparison it is in, so it is carried all the way to the end.
- Repeat on the rest of the list. Stop when a pass makes no swaps, or when only one item is left. Why: no swaps means every neighbour pair is already in order, so the whole list is sorted.
- Why "bubble"? Because the largest item rises to the end like a bubble in water, one place per comparison.
Pass 1 in detail
- 23 and 15: wrong order, swap. List starts 15, 23, ...
- 23 and 31: right order, no swap.
- 31 and 19: swap. Then 31 and 8: swap. Then 31 and 27: swap. Then 31 and 12: swap.
- End of pass 1: 15, 23, 19, 8, 27, 12, 31. That is 6 comparisons and 5 swaps; 31 is now fixed.

What the picture shows
- Each row is one comparison of a neighbouring pair. Orange pairs are swapped, grey pairs are left alone.
- Once 31 is reached it wins every comparison, so it is carried along to the end.
- Pass 1 makes 6 comparisons and 5 swaps, and 31 is now fixed in the last place.
All the passes
| After pass | List | Swaps |
|---|---|---|
| 1 | 15, 23, 19, 8, 27, 12, 31 | 5 |
| 2 | 15, 19, 8, 23, 12, 27, 31 | 3 |
| 3 | 15, 8, 19, 12, 23, 27, 31 | 2 |
| 4 | 8, 15, 12, 19, 23, 27, 31 | 2 |
| 5 | 8, 12, 15, 19, 23, 27, 31 | 1 |
| 6 | 8, 12, 15, 19, 23, 27, 31 | 0 |
- Pass 6 compares 8 and 12 and makes no swaps, so the list is sorted and we stop.
- Comparisons: . Swaps: .
๐ก In plain words
- Each pass fixes one more item at the end, so each pass is one comparison shorter.
- The worst case adds . For 7 items that is 21, exactly what we used here.
- That sum is the arithmetic series you met in A Level Maths: first term 1, last term n โ 1, and n โ 1 terms.
๐ Right vs wrong
- Wrong: stopping as soon as the list looks sorted, without showing a pass. Right: you only know it is sorted after a pass with no swaps, so show that pass (unless only one item is left).
- Wrong: comparing the fixed items again in later passes. Right: each pass stops one place earlier.
โ๏ธ YOUR TURN 1
Try this first:
Use a bubble sort to put 9, 4, 7, 2, 6 into ascending order. Give the list after each pass, and the total comparisons and swaps.
Answer:
| After pass | List | Comparisons | Swaps |
|---|---|---|---|
| 1 | 4, 7, 2, 6, 9 | 4 | 4 |
| 2 | 4, 2, 6, 7, 9 | 3 | 2 |
| 3 | 2, 4, 6, 7, 9 | 2 | 1 |
| 4 | 2, 4, 6, 7, 9 | 1 | 0 |
Totals: 10 comparisons, 7 swaps. Pass 4 makes no swaps, so we stop. 10 is also , the maximum for 5 items, because no pass made zero swaps early.
Shuttle Sort: Slotting Each Item into Place
Bubble sort fixes items at the right. Shuttle sort works the other way round, and it is how most people sort a hand of playing cards: pick up the next card and slide it left until it sits in the right place.
Shuttle sort: the recipe
- Pass 1: compare the 2nd item with the 1st. Swap if needed. The first two items are now in order. Why: a list of two is easy to sort.
- Pass 2: compare the 3rd item with the 2nd. If you swap, compare it with the 1st as well. Stop as soon as no swap is needed. Why: the items to its left are already sorted, so once it meets something smaller it is in the right place.
- Pass k: take the (k + 1)th item and shuttle it left, one comparison at a time, until it meets a smaller (or equal) item or reaches the start.
- A list of n items needs n โ 1 passes. Why: each pass adds one item to the sorted section, which starts with one item.
Unlock the full note: start 7 days free.
Log in or subscribe to read the rest of this note. Members also get the AI tutor, practice quizzes, mock exams and flashcards for this topic.
Log in ยท Subscribe ยท Start free trial
All notes in Algorithms, Complexity, Sorting and Bin Packing
- What Is an Algorithm? Flow Charts, Pseudocode and Trace Tables
- Bubble Sort, Shuttle Sort and Quick Sort Step by Step
- Bin Packing: Lower Bounds, First Fit, First Fit Decreasing and Full Bins
- The Order of an Algorithm: Counting Comparisons and Predicting Run Times
What's inside for this topic
4 teacher notes, 74 practice questions, 52 flashcards, 2 quizzes.
Get the full notes, every practice question, flashcards, quizzes and mock papers.
Previous note: What Is an Algorithm? Flow Charts, Pseudocode and Trace Tables Next note: Bin Packing: Lower Bounds, First Fit, First Fit Decreasing and Full Bins
Bubble Sort, Shuttle Sort and Quick Sort Step by Step on other exam boards
All OCR B (MEI) A Level Further Maths topics ยท A Level Further Maths