Code Companion
Java

Programming technique · B2.4.3

Bubble sort and selection sort

Both algorithms rearrange values into order, but they make progress differently. You need to construct, trace and evaluate both rather than treat sorting as a built-in black box.

IB DP CS standard B2.4.3: Construct and trace bubble sort and selection sort, evaluating their time and space complexities and their advantages and disadvantages across different data sets.

Bubble sort: compare neighbours

Bubble sort repeatedly compares adjacent values and swaps an out-of-order pair. After a complete pass, the largest remaining value has moved to the end of the unsorted section.

First pass from [42, 35, 51, 38, 46]ActionState
42 vs 35swap[35, 42, 51, 38, 46]
42 vs 51keep[35, 42, 51, 38, 46]
51 vs 38swap[35, 42, 38, 51, 46]
51 vs 46swap[35, 42, 38, 46, 51]
After the pass: 51 is in its final position. The whole array is not necessarily sorted yet. The swapped flag allows an already sorted array to stop after one no-swap pass.

Selection sort: choose the smallest remaining value

Selection sort scans the unsorted section to remember the smallest value's index, then places that value at the current start position. The sorted prefix grows by one position per pass.

First pass from [42, 35, 51, 38, 46]smallestIndexReason
start at index 0 (42)0initial candidate
index 1: 35135 < 42
index 2: 51151 is not smaller than 35
index 3: 38138 is not smaller than 35
index 4: 46146 is not smaller than 35
place smallest1swap positions 0 and 1 → [35, 42, 51, 38, 46]

Check your understanding

Answer each question before opening the model answer.

  1. After one bubble-sort pass, what can you say with confidence?

    Reveal model answer

    The largest value in the section processed by that pass has moved to the end of that unsorted section.

  2. What does smallestIndex store during selection sort?

    Reveal model answer

    The index of the smallest value found so far in the current unsorted section.

  3. Why can optimized bubble sort have O(n) best-case time?

    Reveal model answer

    If the data is already sorted, one pass makes no swaps, so the algorithm stops after a single linear scan.

  4. Does selection sort become O(n) on already sorted input?

    Reveal model answer

    No. It still scans the remaining unsorted section on every pass, so its comparison growth remains O(n^2).

Evaluate rather than declare a winner

EvidenceOptimized bubble sortSelection sort
Best-case timeO(n) after a no-swap passO(n^2) comparisons
Average/worst timeO(n^2)O(n^2)
Auxiliary spaceO(1)O(1)
Value movementMay perform many adjacent swapsAt most one placement swap per outer pass in this model
Already/nearly sorted dataEarly exit can helpStill scans remaining candidates
Neither algorithm is universally better. Tie the recommendation to the data pattern and the resource or operation that matters in the scenario.

Test the algorithm, not only the final output

Use unsorted, sorted, reverse-order, single-element and empty data. A final sorted result is necessary, but tracing the passes is what proves you understand how the named algorithm achieved it.

Challenges Choose one

Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.

Build Both Sorts

Challenge ID: PC-T15-C01 · Standards: B2.4.3

Construct bubbleSort and selectionSort without using library sorting. Test each method with unsorted, already sorted, reverse-order, single-element and empty arrays. Hand-trace at least one complete pass of each algorithm before running the program, then confirm the trace against the code.

Scaffold available

Sort Operation Counter

Challenge ID: PC-T15-C02 · Standards: B2.4.3, B2.4.1

Instrument both sorting methods with counters for comparisons and swaps. Run the same unsorted, sorted and reverse-order data through both algorithms. Use the evidence to explain optimized bubble sort's O(n) best case, the O(n^2) average/worst growth of bubble sort, selection sort's O(n^2) comparison growth, and O(1) auxiliary space for both in-place models.

Sorting Recommendation

Challenge ID: PC-T15-C03 · Standards: B2.4.3, B2.4.1

Create a small program that can sort the same data with either bubble sort or selection sort. Compare their behaviour on nearly sorted and reverse-order inputs, then write a balanced recommendation for two different scenarios. Your judgement must discuss input order, comparisons, value movement/swaps, time complexity and auxiliary space rather than claiming one algorithm is universally better.

HL extension: recursion is the next HL-only programming technique. SL students continue directly to file processing.

HL students: continue to recursion →