Slide 2
Mid-Term Feedback & Quick Recap
4 minutes
- Most common mid-term slips
- total / n gave 77, not 77.5
- for (i = 0; i <= n; i++) — one past the end
- 'Not found' printed inside the search loop
- Maximum started at 0
L19 homework 3
- Today
- Lecture 18 found the smallest value in a list. Today we use the same idea again and again to put the WHOLE list in order — sorting.
Recap
Programming Fundamentals · Lecture 20
Slide 3
Today's Plan
50 minutes · 9 parts
Mid-term feedback & recap
4 min
Learning outcomes
2 min
Why sort? The swap
6 min
Bubble sort
12 min
Selection sort
10 min
Comparing sorts & descending order
5 min
Class activity
6 min
Common mistakes & recap
4 min
Homework & next lecture
1 min
Programming Fundamentals · Lecture 20
Slide 4
Learning Outcomes (SLOs)
By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)
Explain what sorting is and why a sorted list is useful
SLO 20.1 → CLO1 · C2
Trace and write bubble sort and selection sort, swapping with a temporary variable
SLO 20.2 → CLO4 · C3
SLO 20.3 → CLO1 · C4
Compare the two sorts by passes, comparisons and swaps, and change them to sort in descending order
- CLO1 Identify the necessary properties of good problem-solving techniques.
- CLO4 Describe the syntax and semantics of the C++ programming language.
Programming Fundamentals · Lecture 20
Slide 5
What Is Sorting and Why?
Arranging a list in ascending (small → large) or descending (large → small) order
→
unsorted
ascending
- Sorted lists in real life
- UAF merit list (highest aggregate first)
- PSL points table
- Phone contacts A → Z
- Shop price list, cheapest first
- After sorting (ascending)
- smallest = a[0]
- largest = a[n − 1]
- median = a[n / 2] (odd n)
- top 3 (descending) = a[0], a[1], a[2]
Module 1 · Why Sort?
Programming Fundamentals · Lecture 20
Slide 6
The Swap: Exchanging Two Values
Every sort is built from one action — swapping two elements
swap.cpp (main part)
swap_bug.cpp (main part)
- int a = 64, b = 25;
- int temp = a; // 1. keep a safe
- a = b; // 2. a gets b
- b = temp; // 3. b gets the old a
- a[0] = a[1]; // 64 is lost!
- a[1] = a[0];
Output
Output — wrong
a = 25, b = 64
25 25
Three glasses: temp ← a, a ← b, b ← temp.
Module 1 · Why Sort?
Programming Fundamentals · Lecture 20
Slide 7
Bubble Sort: The Idea
Compare each pair of neighbours; swap if they are in the wrong order
start
swap
- Pass 1
- 4 comparisons, 4 swaps
- Largest (64) is now at the end — in its final place.
after j=0
swap
after j=1
swap
after j=2
swap
- Then…
- Pass 2 stops one earlier: 3 comparisons.
- n values need n − 1 passes.
after j=3
Module 2 · Bubble Sort
Programming Fundamentals · Lecture 20
Slide 8
Bubble Sort in C++
Outer loop = passes · inner loop = neighbour comparisons
bubble.cpp (main part)
Output
- for (int pass = 1; pass < N; pass++) { // N - 1 passes
- for (int j = 0; j < N - pass; j++) { // neighbours
- if (a[j] > a[j + 1]) { // wrong order?
- int temp = a[j]; // swap
- a[j] = a[j + 1];
- a[j + 1] = temp;
- }
- }
- }
- After pass 1: 25 12 22 11 64
- After pass 2: 12 22 11 25 64
- After pass 3: 12 11 22 25 64
- After pass 4: 11 12 22 25 64
- Comparisons
- 4 + 3 + 2 + 1 = 10
j < N − pass keeps a[j + 1] inside the array AND skips the values already in place.
Module 2 · Bubble Sort
Programming Fundamentals · Lecture 20
Slide 9
Stopping Early: The 'Swapped' Flag
If a whole pass makes no swap, the list is already sorted
bubble_flag.cpp (main part)
Output for 11 12 22 25 64 70
- for (int pass = 1; pass < N; pass++) {
- bool swapped = false; // flag
- for (int j = 0; j < N - pass; j++) {
- if (a[j] > a[j + 1]) {
- // swap a[j] and a[j + 1]
- swapped = true;
- }
- }
- passes++;
- if (!swapped) break; // already sorted
- }
Passes used: 1
- Without the flag
- 5 passes, 15 comparisons — all wasted on a sorted list.
Reset the flag inside the outer loop — once per pass.
Module 2 · Bubble Sort
Programming Fundamentals · Lecture 20
Slide 10
Selection Sort: The Idea
Find the smallest of the unsorted part; swap it into the next place
start: min 11 at [4]
- Key
- amber = smallest found
- green = in final place
pass 1 → min 12 at [2]
pass 2 → min 22 at [3]
pass 3 → min 25 at [3]
- At most one swap per pass
- 4 swaps for 5 values.
- Bubble sort may swap after every comparison.
pass 4: sorted
Module 3 · Selection Sort
Programming Fundamentals · Lecture 20
Slide 11
Selection Sort in C++
Inner loop = 'minimum with position' from Lecture 18
selection.cpp (main part)
Output
- for (int i = 0; i < N - 1; i++) {
- int minPos = i; // smallest so far
- for (int j = i + 1; j < N; j++)
- if (a[j] < a[minPos]) minPos = j;
- int temp = a[i]; // one swap
- a[i] = a[minPos];
- a[minPos] = temp;
- }
- After pass 1: 11 25 12 22 64
- After pass 2: 11 12 25 22 64
- After pass 3: 11 12 22 25 64
- After pass 4: 11 12 22 25 64
- minPos = i
- reset in EVERY pass
Same comparisons as bubble sort (10 for 5 values) but far fewer swaps.
Module 3 · Selection Sort
Programming Fundamentals · Lecture 20
Slide 12
Comparing the Two Sorts · Descending Order
Change one comparison to sort largest first
descending.cpp
- if (runs[j] < runs[j + 1])
- // swap → largest first
Output
- Highest first: 102 88 67 50 45 12
- Top 3: 102, 88, 67
- Grows fast
- 10 values → 45 comparisons; 100 values → 4950.
Ascending: > · Descending: <
Module 4 · Comparing & Descending
Programming Fundamentals · Lecture 20
Slide 13
CLASS ACTIVITY · 6 MIN · IN PAIRS
Be the Computer
Write the array after each pass. Then check with bubble.cpp and selection.cpp.
Tasks
Hints
- 1. Bubble sort {5, 1, 4, 2, 8}: the array after pass 1?
- 2. Selection sort {29, 10, 14, 37, 13}: the array after pass 1 and pass 2?
- 3. How many comparisons does bubble sort (no flag) make for 6 values?
- 4. Which comparison sorts the scores largest first in selection sort?
- 1: neighbours, 4 checks
- 2: smallest of the rest
- 3: 5 + 4 + 3 + 2 + 1
- 4: look for the maximum
Slide 14
Common Mistakes in Sorting
Most sort bugs come from the swap and the loop limits
Test with: already sorted · reverse order · equal values · one element.
Module 5 · Mistakes
Programming Fundamentals · Lecture 20
Slide 15
Quick Recap
Answer aloud
Q1
Why do we need a temp variable to swap?
Q2
After pass 1 of bubble sort, which value is in its final place?
Q3
What does selection sort search for in each pass?
Q4
How do you make bubble sort sort in descending order?
Module 5 · Mistakes
Programming Fundamentals · Lecture 20
Slide 16
Homework
Sort {72, 45, 91, 66, 58, 84, 70} with selection sort and print the median (the middle value).
Write the bubble-sort passes for {5, 4, 3, 2, 1} by hand. How many swaps happen in total?
Read n prices (1–20), sort them cheapest first and print the three cheapest.
Change bubble_flag.cpp to count and print the number of swaps as well as the passes.
Next lecture: 2D arrays — lists of lists (tables) and their memory layout