IB Computer Science HL · Year 2 · Lesson 8

Bubble and selection sort

Algorithms must be understood operationally: how they behave, how to trace them, and when their efficiency makes them suitable.

Paper 250 minutesB2.4.3
Today’s targets

What you need to be able to do

2 / 9
  • Construct + trace + evaluateAnd trace algorithms to implement bubble sort and selection sort, evaluating their time and space complexities
B2.4.3
Paper 2 lensMatch the depth of every response to the command term. Previously learned content can move quickly, but retrieval must still be accurate.
Retrieve

Rapid Recall Deck

3 / 9

Say the answer aloud before flipping. Mark secure knowledge quickly and spend time on the gaps.

  • What does bubble sort do during a pass through a list?: It compares adjacent items and swaps them when they are in the wrong order, causing larger or smaller values to move toward one end.
  • What does selection sort do during each pass?: It finds the smallest or largest remaining item and places it into its next correct position.
  • What is the key operational difference between bubble sort and selection sort?: Bubble sort repeatedly swaps adjacent items; selection sort repeatedly selects an extreme value and usually performs fewer swaps.
  • How do basic bubble sort and selection sort scale?: Their time grows quadratically, O(n²), for typical/worst-case analysis, so both become inefficient on large data sets.
  • What space characteristic do basic in-place bubble and selection sorts share?: They use only a small fixed amount of additional memory, commonly described as O(1) auxiliary space.
B2.4.3 · Learn

Core knowledge and application

4 / 9
B2.4.3Construct + trace + evaluate

And trace algorithms to implement bubble sort and selection sort, evaluating their time and space complexities

Algorithms must be understood operationally: how they behave, how to trace them, and when their efficiency makes them suitable.

Exam moveProduce the required code, diagram, query, model or representation accurately.
Required detail 1The time and space complexities of each algorithm, denoted by their respective Big O notations
Required detail 2The advantages and disadvantages of each algorithm in terms of efficiency across various data sets
Assessment boundaryPaper 2

Explain it without notes

Construct + trace + evaluate: And trace algorithms to implement bubble sort and selection sort, evaluating their time and space complexities in the context of a program that searches and organizes a growing dataset.

  • The time and space complexities of each algorithm, denoted by their respective Big O notations
  • The advantages and disadvantages of each algorithm in terms of efficiency across various data sets
Trace / construct

Programming checkpoint

5 / 9
for end in range(len(data)-1, 0, -1):
    for i in range(end):
        if data[i] > data[i+1]:
            data[i], data[i+1] = data[i+1], data[i]
Before you run itTrace the code by hand. Identify state changes, branch/loop behaviour, and the final result.

Modify it

Change one condition, input or operation so the program solves a slightly different problem. Predict the effect before editing.

Worked example · B2.4.3

Bubble sort vs selection sort

6 / 9

Bubble sort idea

Compare adjacent items and swap those that are out of order. After each full pass, one large item has “bubbled” toward its final position.

[5, 2, 4, 1] → [2, 4, 1, 5] → [2, 1, 4, 5] → [1, 2, 4, 5]

Selection sort idea

Find the smallest item in the unsorted region and swap it into the next fixed position.

[5, 2, 4, 1] → [1, 2, 4, 5] → [1, 2, 4, 5] → [1, 2, 4, 5]
for start in range(len(data) - 1):
    min_i = start
    for i in range(start + 1, len(data)):
        if data[i] < data[min_i]:
            min_i = i
    data[start], data[min_i] = data[min_i], data[start]
Criterion
Bubble
Selection
Typical simple time
O(n²)
O(n²)
Auxiliary space
O(1)
O(1)
Useful distinction
Many adjacent swaps
At most one main swap per outer pass
Apply

Transfer to a new scenario

7 / 9
ScenarioA program that searches and organizes a growing dataset needs a design or technical decision related to today’s topic. Explain what matters and why.
  • The time and space complexities of each algorithm, denoted by their respective Big O notations
  • The advantages and disadvantages of each algorithm in terms of efficiency across various data sets
Exam lens

Paper 2 practice

8 / 9
Build the response before checking notesUse precise terminology and match the required depth.
  1. Construct + trace + evaluate: And trace algorithms to implement bubble sort and selection sort, evaluating their time and space complexities in the context of a program that searches and organizes a growing dataset.
Self-checkAnswer the exact command term. For explain, include mechanism/reason; for compare, pair criteria; for discuss/evaluate/justify, build supported reasoning and a conclusion.
Homework

Finish the learning cycle

9 / 9

IA — main task

Refine Criteria A/B/C. Use today’s lesson to make the problem specification, decomposition, algorithms or testing plan more precise where relevant.

Syllabus — 10–15 min

Repeat the recall deck and complete the lesson response prompt without model support.