Data Structures · Sorting

Bubble Sort Desk Check — 16 names, step by step (Exercise 2)

🔤 The array listElement[16] (names)

comparing i with i+1just swappedthis pass's maximum, now in placenumComparisons = pairs checked this pass
current array

🧾 The desk-check pro forma (one row per pass)

💬 Current step

💻 The algorithm (the courseware listing, verbatim)

Yellow = the line being executed. The recipe: set swapMade to FALSE at the start of each pass → compare pairs and swap (setting swapMade) → decrement numComparisons at the end of the pass → finish when a pass made no swap.

📄 Key points & how to fill in the pro forma

1swapMade = TRUE, numComparisons = sizeOfList − 1
2Each pass: compare i = 0 … numComparisons−1, swap when out of order and set swapMade
3End of pass: Decrement numComparisons (the settled maximum needs no more checks)
4A pass with no swap (swapMade = FALSE) → the WHILE ends, the sort is done
5Hand in: one row per pass with the whole array, plus the two totals at the bottom
How do I fill in the pro forma? One row = one pass: copy the whole 16-name array as it stands after that pass. Then put the total comparisons in the bottom-left box and the total swaps in the middle box. Every row of the table on this page can be copied straight over — the “Copy the table” button even copies it as TSV for Word/Excel.
Why one comparison fewer each pass? At the end of a pass the largest element has been pushed to the end of the unsorted part — it is already in place, so it never needs checking again, hence numComparisons is decremented.
Two optimisations: (1) swapMade stops early on data that is already sorted (best case: 1 pass, 15 comparisons); (2) decrementing numComparisons saves one comparison per pass, bringing the total down from n² to n(n−1)/2.
This data set: 13 passes · 117 comparisons · 87 swaps; during pass 13 swapMade stays FALSE, so the loop ends.
Stability: only a strict inversion (>) swaps, so equal values never cross → bubble sort is stable.

📋 Desk-check pro forma (one row per pass)

Speed
Shortcuts: Space play/pause · ←→ step · R reset · E result