Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
This Comparing the number of comparisons in bubble and selection sort worksheet is part of the WA Computer Science ATAR Year 11 — Standard Algorithms journey. Print it as a booklet?
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1n * (n - 1) / 2 | AUNTIL swapped = FALSE | |
| 2n: 10 -> 20 | BComparisons do not just double. They grow with n squared, so doubling n gives about four times the comparisons (45 to 190). | |
| 3Bubble sort best case: on an already sorted list, the swapped flag lets it stop after one pass of n - 1 comparisons | CThe number of comparisons selection sort always makes on n items. For n = 10 that is 45 comparisons. | |
| 4Selection sort makes at most one swap per pass, so at most n - 1 swaps in total | DDoubling the list size makes about four times as many comparisons: 45 becomes 190. | |
| 5n goes from 10 to 20, so comparisons go from 45 to 90 | ESWAP A[i], A[min] after the inner FOR loop | |
| 6Selection sort comparisons ≈ n | FSelection sort still makes about n squared / 2 comparisons. Its advantage over bubble sort is fewer swaps, not fewer comparisons. |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
O(n^2) — Bubble and selection sort both make about n squared comparisons, so doubling n quadruples the work, but selection sort makes far fewer swaps.comparisons ← n * (n - 1) / 2newWork ← 4 * oldWork
// when n is doubledIF A[j] > A[j + 1] THEN
SWAP A[j], A[j + 1]
swapped ← TRUEswapped ← TRUE
WHILE swapped = TRUE
swapped ← FALSEFOR i FROM 1 TO n - 1
min ← index of smallest in A[i..n]
SWAP A[i], A[min]