← VCE Software Development U3O1

Selection sort in Python

A sorting algorithm that uses nested loops to find the smallest remaining item, then swaps it into place with a, b = b, a. It repeats for each position until the list is sorted.

Understanding Selection sort in Python in VCE Software Development U3O1

What it does: Starting at i + 1 means the inner loop only checks items that are not sorted yet, looking for the minimum. Comparing nums[j] with the current smallest nums[m] and updating m tracks where the minimum is. The outer loop picks each position (0, 1, 2 ...) that needs the correct item placed into it. Python can swap in a single line with a, b = b, a. Using two separate assignments would overwrite one value and lose it. The smallest item (1) is found and swapped with the item in position 0 (5), giving [1, 2, 9, 5]. The a, b = b, a pattern swaps two items in one line, which puts the minimum into its correct place.

Common mistakes: The inner loop must only check the unsorted items after position i, so it should start at i + 1. Starting at i - 1 looks back at the sorted part (and at -1 when i is 0). m holds the index of the smallest item found so far in the unsorted part. It must start at i, the current position, not at 0, which is the start of the whole list.

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 142–155) and word search (Modules 142–166).

Take this worksheet further — free with Cloodit

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

Learning
Duo
What does the inner loop of a selection sort do?
Finds the smallest item in the unsorted part of the listSwaps every pair of neighbouring items
Matches
nums[i] = nums[m] nums[m] = nums[i]
↓
This is a bad swap. The first line overwrites nums[i], so its original value is lost and both items end up the same.
Applying
Meanings
Term
selection sort
Meaning
A sorting algorithm that uses nested loops to find the smallest remaining item, then swaps it into place with a, b = b, a. It repeats for each position until the list is sorted.
Practice
scores = [64, 25, 12, 22, 11]
Testing
Gaps
for i in (len(nums)):
Check Answer
Spots
2 m = i
3 for j in range(i - 1, n):

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

The 'Dolphin' badge — Reef Streak, 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. What does the inner loop of a selection sort do?
A. Swaps every pair of neighbouring items
B. Finds the smallest item in the unsorted part of the list ✓
The inner loop scans the remaining items to find the position of the minimum. The swap happens after the scan.
2. What does the outer loop of a selection sort go through?
A. Each position in the list, one at a time ✓
B. Only the last position in the list
The outer loop picks each position (0, 1, 2 ...) that needs the correct item placed into it.
3. Which line correctly swaps two items in a Python list?
A. nums[i], nums[m] = nums[m], nums[i] ✓
B. nums[i] = nums[m] and nums[m] = nums[i]
Python can swap in a single line with a, b = b, a. Using two separate assignments would overwrite one value and lose it.
4. After the first pass of a selection sort on [5, 2, 9, 1], what is the list?
A. [2, 5, 1, 9]
B. [1, 2, 9, 5] ✓
The smallest item (1) is found and swapped with the item in position 0 (5), giving [1, 2, 9, 5].
5. Which inner loop correctly checks only the unsorted items after position i?
A. for j in range(i + 1, len(nums)): ✓
B. for j in range(0, i):
The inner loop starts at i + 1 so it only looks at items that are not yet sorted. range(0, i) would look at the sorted part.

Match the Pairs

for i in range(len(nums)): → The outer loop: it goes through each position in the list, one at a time, to fill with the smallest remaining item.
nums[i], nums[m] = nums[m], nums[i] → Swaps the item at position i with the smallest item found at position m.
The inner loop searches the unsorted part of the list to find the position of the smallest item. → for j in range(i + 1, len(nums)):
When the inner loop finds a smaller item, remember its position as the new minimum. → if nums[j] < nums[m]: m = j
nums[i] = nums[m] nums[m] = nums[i] → This is a bad swap. The first line overwrites nums[i], so its original value is lost and both items end up the same.
for j in range(i, len(nums)): m = j → The inner loop always sets m to j without comparing, so m just ends up as the last position instead of the smallest item.

Key Term

selection sort
A sorting algorithm that uses nested loops to find the smallest remaining item, then swaps it into place with a, b = b, a. It repeats for each position until the list is sorted.

Fill in the Blanks

1. for i in range(len(nums)): smallest = i
Hint: Type the function that makes the outer loop count through each position in the list.
2. for j in range(i + 1, len(nums)):
Hint: Type the expression for where the inner loop starts: the position just after the current one.
3. if nums[j] < nums[smallest]: smallest = j
Hint: Type the comparison operator that checks whether the item at j is smaller than the current smallest.
4. a[i], a[m] = a[m], a[i]
Hint: Type the variable that holds the index of the smallest item, so the two items trade places.
5. for i in range(len(nums)): smallest = i for j in range(i + 1, len(nums)):
Hint: Type the variable that makes us assume the current position holds the smallest item before searching.

Spot the Error

Question 1
A for i in range(n - 1):
B m = i
C for j in range(i - 1, n):
D if a[j] < a[m]:
E m = j
F a[i], a[m] = a[m], a[i]
Line C — The inner loop must only check the unsorted items after position i, so it should start at i + 1. Starting at i - 1 looks back at the sorted part (and at -1 when i is 0).
Question 2
A n = len(a)
B for i in range(n - 1):
C m = 0
D for j in range(i + 1, n):
E if a[j] < a[m]: m = j
F a[i], a[m] = a[m], a[i]
Line C — m holds the index of the smallest item found so far in the unsorted part. It must start at i, the current position, not at 0, which is the start of the whole list.
Question 3
A a = [4, 1, 3]
B a[0], a[1] = a[0], a[1]
C print(a)
Line B — This line does not swap anything because both values are put back where they came from. A swap needs the right side reversed: a[0], a[1] = a[1], a[0].
Question 4
A for i in range(n - 1)
B m = i
C for j in range(i + 1, n):
D if a[j] < a[m]:
E m = j
F a[i], a[m] = a[m], a[i]
Line A — A for statement must end with a colon. It should be: for i in range(n - 1):
Question 5
A n = length(a)
B for i in range(n - 1):
C m = i
D for j in range(i + 1, n):
E if a[j] < a[m]: m = j
F a[i], a[m] = a[m], a[i]
Line A — Python has no length() function for lists. The built-in is len(), so the line should be n = len(a).

Student with a class code? Join your class →