← WA Computer Science ATAR Unit 1

Tracing a selection sort

A table of the array after each selection sort pass: the minimum of the unsorted part is swapped to the left, so the sorted part grows by one each pass.

Understanding Tracing a selection sort in WA Computer Science ATAR Unit 1

What it does: Swapping 1 with the 5 at the left gives [1, 2, 8, 5], and the sorted part has grown to one item. The minimum is 1 at index 4. It is swapped with the first item (5), giving [1, 3, 8, 5]. Only one swap happens. In pass 2 the minimum of [2, 7, 4] is 2, already in place, so the array stays the same. The 4 and 7 are only fixed in pass 3. Each pass begins by treating position i, the left end of the unsorted part, as the index of the minimum until a smaller value is found.

Common mistakes: Selection sort makes one swap per pass. The minimum (1) swaps places with the first item (5), giving [1, 3, 8, 5]. The other items do not shift along. Pass 1 makes one swap: the minimum (1) swaps with the first item (4). That gives [1, 2, 7, 4]. The 2 stays where it is.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 100–314) and word search (Modules 100–470).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
In a selection sort trace, where does the sorted part of the array grow from?
The right end of the arrayThe left end of the array
Matches
Pass 1 of [4, 3, 1, 2] traced as [3, 4, 1, 2]
↓
This swaps only the first two items, as a bubble sort would. Selection sort swaps the first item with the minimum, 1, giving [1, 3, 4, 2].
Applying
Meanings
Term
trace table
Meaning
A table of the array after each selection sort pass: the minimum of the unsorted part is swapped to the left, so the sorted part grows by one each pass.
Practice
Pass 0: 5 3 8 1
Testing
Gaps
FOR i ← 1 TO
Check Answer
Spots
1Start: [5, 3, 8, 1]
2Pass 1: [1, 5, 3, 8]

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Butterfly' badge — Branch Hopper, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. In a selection sort trace, where does the sorted part of the array grow from?
A. The right end of the array
B. The left end of the array ✓
Selection sort moves the minimum of the unsorted part to the left, so the sorted part grows from the left by one item each pass.
2. Trace one pass of selection sort on [5, 3, 8, 1]. What does the array look like after pass 1?
A. [1, 3, 8, 5] ✓
B. [3, 5, 1, 8]
The minimum is 1 at index 4. It is swapped with the first item (5), giving [1, 3, 8, 5]. Only one swap happens.
3. How many swaps does selection sort make in each pass?
A. One swap per pass ✓
B. One swap for every pair compared
Selection sort scans the unsorted part to find the index of the minimum, then makes just one swap at the end of the pass.
4. Array after pass 1 is [1, 6, 4, 9, 2]. Which item will be swapped into position 2 on pass 2?
A. 4, because it is next to the 6
B. 2, because it is the minimum of [6, 4, 9, 2] ✓
Pass 2 searches only the unsorted part [6, 4, 9, 2]. The minimum is 2, which is swapped with the 6 to give [1, 2, 4, 9, 6].
5. During a pass, what does a trace table record besides the array?
A. The index of the minimum found in the unsorted part ✓
B. The index of the largest item in the whole array
Selection sort tracks the index of the minimum of the unsorted part. The swap then puts that item at the start of the unsorted part.

Match the Pairs

minIndex ← i → Assume the first unsorted item is the smallest at the start of each pass
SWAP numbers[i], numbers[minIndex] → Make the one swap for this pass, putting the minimum at the end of the sorted part
Trace of [5, 2, 8, 1] after pass 1: the minimum 1 at index 4 is swapped with the first item, so the sorted part is [1] → [1, 2, 8, 5]
After pass 2 of [1, 2, 8, 5], the minimum of the unsorted part is already in place, so the swap changes nothing and the sorted part grows to two → [1, 2, 8, 5]
Pass 1 of [4, 3, 1, 2] traced as [3, 4, 1, 2] → This swaps only the first two items, as a bubble sort would. Selection sort swaps the first item with the minimum, 1, giving [1, 3, 4, 2].
Trace row for pass 2 shows the sorted part shrinking from [1, 2] to [1] → The sorted part grows by one item each pass from the left, so after pass 2 it holds two items and never shrinks.

Key Term

trace table
A table of the array after each selection sort pass: the minimum of the unsorted part is swapped to the left, so the sorted part grows by one each pass.

Fill in the Blanks

1. FOR i ← 1 TO n - 1 minIndex ← i FOR j ← i + 1 TO n
Hint: Type the expression for the last pass number. One fewer than the array size, because the final item is already in place.
2. FOR i ← 1 TO n - 1 minIndex ← i FOR j ← i + 1 TO n
Hint: Type the variable that holds the first position of the unsorted part, which is the starting guess for the minimum.
3. IF data[j] < data[minIndex] THEN minIndex ← j ENDIF
Hint: Type the comparison operator that checks whether the current item is smaller than the minimum found so far.
4. temp ← data[i] data[i] ← data[minIndex] data[minIndex] ← temp
Hint: Type the variable holding the index of the smallest item. It is swapped into the front of the unsorted part, once per pass.
5. Start: [5, 3, 8, 1] Pass 1: [1, 3, 8, 5] Pass 2: [1, 3, 8, 5]
Hint: Type the value that the swap moves into the first position of the trace after pass 1: the minimum of the whole array.

Spot the Error

Question 1
A Start: [5, 3, 8, 1]
B Pass 1: [1, 5, 3, 8]
C Pass 2: [1, 3, 8, 5]
D Pass 3: [1, 3, 5, 8]
Line B — Selection sort makes one swap per pass. The minimum (1) swaps places with the first item (5), giving [1, 3, 8, 5]. The other items do not shift along.
Question 2
A Start: [4, 2, 7, 1]
B Pass 1: [1, 4, 7, 2]
C Pass 2: [1, 2, 7, 4]
D Pass 3: [1, 2, 4, 7]
Line B — Pass 1 makes one swap: the minimum (1) swaps with the first item (4). That gives [1, 2, 7, 4]. The 2 stays where it is.
Question 3
A FOR i ← 2 TO n - 1
B min ← i
C FOR j ← i + 1 TO n
D IF A[j] < A[min] THEN
E min ← j
F swap A[i], A[min]
Line A — The outer loop must start at 1 so the first pass puts the smallest item at index 1. Starting at 2 skips that pass. It should be FOR i ← 1 TO n - 1.
Question 4
A Array: [6, 2, 9, 4]
B Pass 1: min is 2 at index 2
C Pass 2: min is 2 at index 1
D Pass 3: min is 6 at index 4
Line C — After pass 1 the array is [2, 6, 9, 4] and index 1 is already sorted. Pass 2 searches only the unsorted part, where the minimum is 4 at index 4.
Question 5
A min ← A[i]
B FOR j ← i + 1 TO n
C IF A[j] < A[min] THEN
D min ← j
Line A — The variable min stores the index of the smallest item, not its value. It is compared using A[min], so it must start as min ← i.

Student with a class code? Join your class →