Procedural Programming with C++
Spot it
Spot the bubble sort mistake
How many passes can bubble sort need to sort five marks?
Quick check · tap an answer to continue
Lesson notes
Use nested loops over 2D arrays and implement linear search and bubble sort
Key points
- Pass 1 compares neighbours and swaps, giving {1, 4, 2, 5, 8}; 8 is in place.
- Pass 2 swaps 4 and 2, giving {1, 2, 4, 5, 8}.
- After each pass the largest unsorted value is in place, so n−1 = 4 passes are the worst case.
- Pass 3 makes no swaps, so the list {1, 2, 4, 5, 8} is sorted.
Takeaway: Bubble sort needs at most n−1 passes and stops when nothing swaps.
Quick check: Which step is wrong?
- Step 1
- Step 2
- Step 3
Each pass fixes one element, so n−1 passes suffice; a no-swap pass allows early stop.
2D arraylinear searchbubble sort
Lessons like this, for your syllabus.
Short narrated study videos written for your course, level and exam. Free to start.
Get lessons for your own syllabus