Bubble Sort and Quick Sort Step by Step | Edexcel IAL Maths notes
Free preview: Bubble Sort and Quick Sort Step by Step
Bubble Sort: Let the Biggest Rise
Unit D1 (WDM11): Decision Mathematics 1
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 both 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 your earlier 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.
Quick Sort: Pivots and Sublists
Bubble sort creeps along one neighbour at a time. On a long list that is slow. Quick sort uses a smarter idea: divide and conquer. Split the list into a "small" pile and a "big" pile, then sort each pile the same way.
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 D1.1 Algorithms, Sorting, Searching and Packing
- What Is an Algorithm? Flow Charts, Pseudocode and Trace Tables
- Bubble Sort and Quick Sort Step by Step
- Binary Search: Halving an Ordered List
- Bin Packing: Lower Bounds, First Fit, First Fit Decreasing and Full Bins
What's inside for this topic
4 teacher notes, 103 practice questions, 38 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: Binary Search: Halving an Ordered List