UNIT 2 • MODULE 1 • LESSON 10
You now know two sorting algorithms. Both put a list in order. Both use two nested loops and a three-step swap. Both make about the same number of comparisons on a random list. So does it matter which one you use? It does, and knowing when each one is the better choice is exactly the kind of judgement CAPE examiners reward.
In this lesson you will put selection sort and bubble sort side by side, count their comparisons and swaps on different kinds of data, race them in a Try it, and practise the “which would you choose, and why?” questions that turn up in Paper 02.
What you will learn
- How selection sort and bubble sort differ in what they compare, what they move and where the sorted part grows.
- The number of comparisons and swaps each makes in the best, worst and typical cases.
- How the kind of data (random, reversed, sorted, nearly sorted) changes the result.
- How to choose the better sort for a given situation and justify your choice.
- How to answer comparison questions clearly in an exam.
Part 1: Side by side
Here are the two algorithms next to each other. Read them line by line and spot the differences.
Selection sort
FOR i ← 0 TO n - 2
minIdx ← i
FOR j ← i + 1 TO n - 1
IF list[j] < list[minIdx] THEN
minIdx ← j
ENDIF
ENDFOR
IF minIdx ≠ i THEN
swap list[i], list[minIdx]
ENDIF
ENDFOR
Bubble sort (with flag)
FOR pass ← 0 TO n - 2
swapped ← FALSE
FOR j ← 0 TO n - 2 - pass
IF list[j] > list[j + 1] THEN
swap list[j], list[j + 1]
swapped ← TRUE
ENDIF
ENDFOR
IF NOT swapped THEN EXIT
ENDFOR
| Feature | Selection sort | Bubble sort |
| Main idea | Select the smallest, swap it into place | Swap neighbours that are out of order |
| Compares | Each item with the smallest so far | Each item with its right neighbour |
| Swaps per pass | At most 1 | Up to the length of the unsorted part |
| Sorted part grows from | The left (smallest first) | The right (largest first) |
| Can stop early? | No | Yes, with a swapped flag |
| Stable? | No | Yes (equal values never swap) |
The key difference is where the work goes. Selection sort spends its effort searching, then makes one decisive swap per pass. Bubble sort mixes searching and moving together: every out-of-order pair it meets is swapped on the spot. That single difference explains almost every result in this lesson.
Bubble sort is stable because it only swaps when the left value is strictly greater, so two equal values never change places. Selection sort’s long-distance swap can jump one value over an equal one. If you sort students by form after sorting them by name, a stable sort keeps the names in order within each form.
Part 2: Counting the work
| Measure (n items) | Selection sort | Bubble sort (with flag) |
| Comparisons, best case | n(n – 1) / 2 | n – 1 (already sorted) |
| Comparisons, worst case | n(n – 1) / 2 | n(n – 1) / 2 |
| Swaps, best case | 0 | 0 |
| Swaps, worst case | n – 1 | n(n – 1) / 2 (reversed) |
| Passes | Always n – 1 | 1 to n – 1 |
Put numbers in for a class of n = 40 students. Selection sort always makes 780 comparisons and at most 39 swaps. Bubble sort with a flag makes anywhere from 39 comparisons (if the list is already sorted) to 780 comparisons and 780 swaps (if it is in reverse order). On random data, bubble sort typically makes about half the maximum number of swaps, around 390, ten times as many as selection sort.
So neither sort is “faster” in every situation. Selection sort is predictable and swap-light. Bubble sort is data-sensitive: brilliant on nearly sorted data, poor on reversed data.
Part 3: The race
Race the two sorts on the same 8 numbers. Before each race, predict which one will make fewer swaps, then step through or race to the end and compare the counters. Your results build up underneath so you can compare data sets.
Try it 1Sort race: selection vs bubble
Choose the data, make your prediction, then step both sorts together. Try every data set, then turn early exit off and race again.
Choose data
Selection sort
Bubble sort
Your race results
Look at your results list. You should see four patterns: on random data the comparisons are close but bubble sort makes far more swaps; on reversed data bubble sort swaps on every comparison; on already sorted data bubble sort with early exit wins easily; and on nearly sorted data bubble sort still wins on comparisons while both make the same tiny number of swaps. Turn early exit off and bubble sort loses its advantage on sorted data, because it then makes all its comparisons too.
Part 4: Measuring in C
This program runs both sorts on copies of the same four data sets and prints the comparisons and swaps for each, as a small table (c/s means comparisons/swaps).
#include <stdio.h>
#define N 8
void selection(int a[], int n,
int *comps, int *swaps)
{
int i, j, m, t;
*comps = *swaps = 0;
for (i = 0; i < n - 1; i++) {
m = i;
for (j = i + 1; j < n; j++) {
(*comps)++;
if (a[j] < a[m])
m = j;
}
if (m != i) {
t = a[i]; a[i] = a[m]; a[m] = t;
(*swaps)++;
}
}
}
void bubble(int a[], int n,
int *comps, int *swaps)
{
int p, j, t, swapped = 1;
*comps = *swaps = 0;
for (p = 0; p < n - 1 && swapped; p++) {
swapped = 0;
for (j = 0; j < n - 1 - p; j++) {
(*comps)++;
if (a[j] > a[j + 1]) {
t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
(*swaps)++;
swapped = 1;
}
}
}
}
int main(void)
{
const char *name[4] = {"Random",
"Reversed", "Sorted", "Nearly"};
int data[4][N] = {
{42, 17, 88, 5, 63, 29, 71, 36},
{80, 70, 60, 50, 40, 30, 20, 10},
{10, 20, 30, 40, 50, 60, 70, 80},
{10, 20, 30, 40, 60, 50, 70, 80}};
int s[N], b[N], k, i, sc, ss, bc, bs;
printf("Data Sel c/s Bub c/s\n");
for (k = 0; k < 4; k++) {
for (i = 0; i < N; i++)
s[i] = b[i] = data[k][i];
selection(s, N, &sc, &ss);
bubble(b, N, &bc, &bs);
printf("%-9s %3d/%-3d %3d/%d\n",
name[k], sc, ss, bc, bs);
}
return 0;
}
Output:
Data Sel c/s Bub c/s Random 28/5 25/13 Reversed 28/4 28/28 Sorted 28/0 7/0 Nearly 28/1 13/1
The numbers confirm the race. Selection sort makes 28 comparisons every time (8 × 7 / 2), and never more than 7 swaps; here it made 5, 4, 0 and 1. Bubble sort’s comparisons range from 7 to 28 and its swaps from 0 to 28. Notice two details in the code. First, each sort works on its own copy of the data (s and b); otherwise the second sort would receive an already sorted list and the comparison would be unfair. Second, both functions report their counts through pointer parameters, a technique you have used since Lesson 03.
Counting operations is a better way to compare algorithms than timing them with a stopwatch. Times depend on the computer, the compiler and what else is running; counts depend only on the algorithm and the data, so anyone can check them by hand.
Part 5: Same list, two sorts
Small examples show the difference most clearly. On the list 9, 4, 7, 1, selection sort finishes the job with a single swap in pass 1 (the 1 and the 9 trade places), while bubble sort needs five swaps because the 1 has to creep one step left in every pass. Step through both in the tracer.
Try it 2Trace both sorts on one list
Run selection sort, then bubble sort, on the same list. Compare the counters at the end.
Output
| After | Selection sort | Bubble sort |
| Pass 1 | 1, 4, 7, 9 | 4, 7, 1, 9 |
| Pass 2 | 1, 4, 7, 9 | 4, 1, 7, 9 |
| Pass 3 | 1, 4, 7, 9 | 1, 4, 7, 9 |
| Totals | 6 comparisons, 1 swap | 6 comparisons, 5 swaps |
Exams often show you a list after some passes and ask which sort produced it. Use the patterns: after pass k of selection sort, the k smallest values sit in order at the start. After pass k of bubble sort, the k largest values sit in order at the end. In the table, only selection sort could have produced 1, 4, 7, 9 after pass 1, and only bubble sort could have produced 4, 7, 1, 9.
Part 6: Which one would you choose?
| Situation | Better choice | Reason |
| Data is already or nearly sorted | Bubble sort with a flag | Stops after a pass with no swaps; few comparisons |
| Swapping is expensive (large records, slow storage) | Selection sort | At most n - 1 swaps |
| Data in reverse order | Selection sort | Bubble sort swaps on every comparison |
| Equal values must keep their order | Bubble sort | It is stable |
| Running time must be predictable | Selection sort | Always n(n - 1) / 2 comparisons |
| Very large data set | Neither, ideally | Both grow with n squared; faster sorts exist |
Which-to-choose exam questions
Try these before opening the model answers. A good answer names the sort and gives a reason linked to the data or the cost of operations.
1. A teacher keeps 2,000 marks in ascending order. Each day a handful of marks are corrected by one or two points, so a few values end up just one or two places out of position. Which sort would you recommend for putting the list back in order, and why?
Show answer
Bubble sort with a swapped flag. Because every value is at most a couple of places from where it belongs, one or two passes of neighbour swaps put the list right, and the next pass makes no swaps, so the flag stops the sort after only a few thousand comparisons. Selection sort would always make about two million comparisons (2,000 × 1,999 / 2). Be careful, though: if a new small value were added at the END of the list, bubble sort would need many passes, because small values move left only one place per pass.
2. Records of 500 students are stored on a slow memory card where every write takes a long time. The records are in random order. Which sort would you recommend?
Show answer
Selection sort. It makes at most 499 swaps, and each swap means writing records, whereas bubble sort on random data typically makes tens of thousands of swaps. The extra comparisons of selection sort only need reads, which are cheaper.
3. A program must sort a list that is known to be in exactly reverse order. Compare the work done by the two sorts on n items.
Show answer
Both make n(n - 1) / 2 comparisons. Selection sort makes about n / 2 swaps (each swap fixes two values), while bubble sort makes n(n - 1) / 2 swaps, one for every comparison. Selection sort is better here.
4. A list of exam candidates is already sorted by name. It must now be sorted by school, keeping candidates from the same school in name order. Which sort should be used?
Show answer
Bubble sort, because it is stable: it never swaps equal values, so candidates from the same school stay in name order. Selection sort could break the name order.
5. A student says, "Bubble sort is always slower than selection sort." Evaluate this statement.
Show answer
It is not always true. On random or reversed data bubble sort usually makes many more swaps, so it is slower. But on sorted or nearly sorted data, bubble sort with a flag makes far fewer comparisons and stops early, so it is faster.
6. After two passes, the list 30, 10, 50, 20, 40 has become 10, 20, 50, 30, 40. Which sort was used? Explain.
Show answer
Selection sort: the two smallest values (10 and 20) are in their final places at the start, and the rest are unsorted. After two passes of bubble sort, the two largest (40 and 50) would be at the end instead.
Part 7: Beyond these two
Selection sort and bubble sort are the two sorts in this syllabus, but they are not the only ones. Insertion sort builds the sorted part by taking each new item and sliding it back into place, rather like sorting cards as you pick them up; it is also excellent on nearly sorted data. Merge sort and quicksort use divide and conquer, the same idea as binary search, and make roughly n × log₂ n comparisons instead of n squared. For a million items that is about 20 million comparisons instead of 500 billion, which is why real software libraries use them. Knowing that faster sorts exist, and why, is a sign of a strong answer when a question asks about very large data sets.
Try it 3Compare and choose drill
Pick a mode. "Choose" gives a situation and asks which sort fits best. "Count" asks for comparisons or swaps for a given n.
Summary
| Idea | Remember |
| Comparisons | Selection: always n(n - 1) / 2. Bubble with flag: n - 1 to n(n - 1) / 2. |
| Swaps | Selection: at most n - 1. Bubble: up to n(n - 1) / 2. |
| Early exit | Bubble sort only, using a swapped flag. |
| Stability | Bubble sort is stable; selection sort is not. |
| After pass k | Selection: k smallest at the start. Bubble: k largest at the end. |
| Choose | Nearly sorted: bubble with flag. Costly swaps or reversed: selection. |
Review questions
1. State one similarity and two differences between selection sort and bubble sort.
Show answer
Similarity: both use nested loops and make n(n - 1) / 2 comparisons in their worst case. Differences: selection sort makes at most one swap per pass while bubble sort can make many; bubble sort can stop early with a flag while selection sort cannot (also: bubble sort is stable, selection is not).
2. For 12 items, give the number of comparisons made by selection sort, and by bubble sort with a flag on sorted data.
Show answer
Selection: 66. Bubble with flag on sorted data: 11.
3. Why does selection sort make the same number of comparisons on sorted and unsorted data?
Show answer
To find the smallest remaining value it must look at every value in the unsorted part, whatever their order. There is no test that lets it stop early.
4. Why can bubble sort make many more swaps than selection sort?
Show answer
Bubble sort swaps every neighbouring pair that is out of order, so a value moves one place per swap. Selection sort moves a value straight to its final place with a single swap.
5. Explain why counting comparisons and swaps is a fair way to compare sorting algorithms.
Show answer
Counts depend only on the algorithm and the data, not on the speed of a particular computer, so they can be checked by hand and compared directly.
6. Starting from 41, 5, 23, 37, 12, one pass of a sort gives 5, 41, 23, 37, 12. Which sort was used?
Show answer
Selection sort. Its first pass swaps the smallest value (5) with the first value (41), which gives exactly this list. One pass of bubble sort would instead carry 41 to the end, giving 5, 23, 37, 12, 41.
7. Give a situation where you would choose bubble sort, with a reason.
Show answer
When the data is already nearly sorted, because bubble sort with a flag stops after a pass with no swaps, needing only a few passes.
8. Give a situation where you would choose selection sort, with a reason.
Show answer
When swaps are expensive, for example moving large records or writing to slow storage, because selection sort makes at most n - 1 swaps.
Homework
- Extend compare_sorts.c with a fifth data set of your own. Predict the counts before you run it.
- Remove the flag from the bubble function in compare_sorts.c. Which results change, and why?
- Write a paragraph answering: "Which sort should a teacher use to order 35 marks typed in random order?" Justify your answer using counts.
- Find a list of 6 numbers on which selection sort and bubble sort make exactly the same number of swaps.
- Research insertion sort. Trace it on 9, 4, 7, 1 and count its comparisons and moves. How does it compare with the two sorts in this lesson?
Mastery quiz: Comparing the sorts
This 10-question quiz is separate from the short Quick quiz at the end of the lesson. Score 8/10 or higher to earn this lesson’s badge. Your best score is saved in this browser on this device.
Mastery quiz: Selection Sort vs Bubble Sort Compared
Your course badges: loading saved progress…
View badges
No course badges earned yet. Score 8/10 in a lesson quiz to earn its badge.
Saved in this browser on this device. Clearing browser data may remove your badges.
Answer all 10 questions. Earn this lesson’s badge at 8/10 or higher. Read the explanations, revisit the Try it activities and retry as often as you need. These are self-study badges, not formal grades.
Exam tips
Comparison questions. Structure your answer as similarity, then differences, then a conclusion tied to the scenario. Use numbers when you can: "selection sort makes at most n - 1 swaps, whereas bubble sort can make up to n(n - 1) / 2" earns more credit than "selection sort swaps less".
Which-to-choose questions. Look for clues in the scenario: nearly sorted, new items added or check if sorted point to bubble sort with a flag; large records, slow writes or reversed point to selection sort; keep the existing order of equal items points to bubble sort because it is stable.
Paper 01 items on passes. When shown a list after some passes, check the ends: smallest values fixed at the start means selection sort; largest values fixed at the end means bubble sort. Lessons 08 and 09 have tips on the specimen items for each sort.
More worked answers are on the CS Solutions page.
Previous: 2009 U1 Q1. Next: Unit 2 • Module 2: Software Engineering (coming soon in this course).
Syllabus link: CAPE Computer Science (CXC A19/U2/21), Data Structures (Unit 2, Module 1). Specific Objective 5: apply searching and sorting algorithms (comparing selection sort and bubble sort).
Video search terms
Search YouTube for: "selection sort vs bubble sort", "sorting algorithms compared visualisation", "stable vs unstable sorting".
Try it 4Quick quiz
Tap an answer. All eight questions are self-checks on this lesson.
Updates
2026/12/01
- First version of the lesson.
© 2026 islandclass.org
