← VCE Software Development U3O1

Quick sort in pseudocode

A recursive function that picks a pivot, partitions the array into smaller and larger parts around it, then calls itself on each part until the array is sorted.

Understanding Quick sort in pseudocode in VCE Software Development U3O1

What it does: Partitioning compares each item with the pivot and sends smaller values to the smaller array. The base case returns the array unchanged, which stops the recursion. The function needs an array parameter so it knows which data to sort. A header with no parameter has nothing to partition. Quick sort begins by choosing a pivot. Everything else is partitioned around that pivot. The recursive calls quickSort(smaller) and quickSort(larger) sort each part separately. The pivot belongs between the sorted smaller and larger parts, or it disappears from the output.

Common mistakes: The loop should only go over the rest of the items. Looping over the whole array compares the pivot with itself and puts it in a part as well. It should be: for each x in rest. The function header has no parameter, so the function has no array to sort and the pivot line cannot work. It should be: function quickSort(array).

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
Which function header correctly starts a pseudocode quick sort that takes an array?
define quickSort(array)define quickSort()
Matches
define quickSort(array) pivot <- array[0] smaller <- quickSort(smaller) larger <- quickSort(larger)
↓
There is no base case, so the function never stops calling itself and the recursion is infinite. It also calls quickSort on arrays that were never partitioned. Add a check for length <= 1 and partition before the calls.
Applying
Meanings
Term
quickSort(array)
Meaning
A recursive function that picks a pivot, partitions the array into smaller and larger parts around it, then calls itself on each part until the array is sorted.
Practice
define quickSort(array)
Testing
Gaps
define quickSort()
Check Answer
Spots
3larger <- []
4for each x in array

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

The 'Oryx' badge — Desert Navigator, 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. Which function header correctly starts a pseudocode quick sort that takes an array?
A. define quickSort(array) ✓
B. define quickSort()
The function needs an array parameter so it knows which data to sort. A header with no parameter has nothing to partition.
2. What is the first main step inside quickSort after checking the array size?
A. Choose a pivot value from the array ✓
B. Join the array back together
Quick sort begins by choosing a pivot. Everything else is partitioned around that pivot.
3. After partitioning around the pivot, where do the values go?
A. Smaller values to one part, larger values to the other ✓
B. All values stay in one part with the pivot
Partitioning splits the array into a smaller part and a larger part, with the pivot between them.
4. Which is the correct base case for quickSort?
A. if length of array = 5 then return array
B. if length of array <= 1 then return array ✓
An array with 0 or 1 items is already sorted, so the recursion stops there. A fixed size like 5 would not stop it for other array sizes.
5. How does quickSort sort each part after partitioning?
A. It calls a different function on the whole array again
B. It calls itself on the smaller part and the larger part ✓
The recursive calls quickSort(smaller) and quickSort(larger) sort each part separately.

Match the Pairs

define quickSort(array) → The function header: it names the function quickSort and takes the array to be sorted as a parameter
quickSort(smaller) → A recursive call that sorts the part of the array holding values less than the pivot
Partition step: values smaller than the pivot are placed in a separate array → if item < pivot then add item to smaller
Base case: an array with 0 or 1 items is already sorted, so the function stops calling itself → if length(array) <= 1 then return array
define quickSort(array) pivot <- array[0] smaller <- quickSort(smaller) larger <- quickSort(larger) → There is no base case, so the function never stops calling itself and the recursion is infinite. It also calls quickSort on arrays that were never partitioned. Add a check for length <= 1 and partition before the calls.
return quickSort(smaller) + quickSort(larger) → The pivot is left out of the result, so it is lost from the sorted array. It should be quickSort(smaller) + [pivot] + quickSort(larger).

Key Term

quickSort(array)
A recursive function that picks a pivot, partitions the array into smaller and larger parts around it, then calls itself on each part until the array is sorted.

Fill in the Blanks

1. define quickSort(array)
Hint: Type the parameter name that receives the list of values to be sorted.
2. if length(array) <= 1 then return array
Hint: Type the comparison operator that checks for one item or fewer, the base case that stops the recursion.
3. pivot ← array[0]
Hint: Type the index of the first element in the array, which is used as the pivot.
4. for each item in rest if item < pivot then add item to smaller
Hint: Type the comparison operator that sends a value into the smaller part during partitioning.
5. left ← quickSort(smaller) right ← quickSort(larger) return left + [pivot] + right
Hint: Type the function's own name so it calls itself on the larger part (recursive call).

Spot the Error

Question 1
A pivot <- array[0]
B smaller <- []
C larger <- []
D for each x in array
E if x < pivot then
F append x to smaller
Line D — The loop should only go over the rest of the items. Looping over the whole array compares the pivot with itself and puts it in a part as well. It should be: for each x in rest.
Question 2
A function quickSort()
B pivot <- array[0]
C partition rest by pivot
D lo <- quickSort(smaller)
E hi <- quickSort(larger)
F return lo + [pivot] + hi
Line A — The function header has no parameter, so the function has no array to sort and the pivot line cannot work. It should be: function quickSort(array).
Question 3
A function split(array pivot)
B small <- items below pivot
C large <- items above pivot
D return small, large
Line A — Parameters in a function header must be separated by a comma. It should be: function split(array, pivot).
Question 4
A function quickSort(array)
B if length(array) >= 1 then
C return array
D end if
E pivot <- array[0]
F smaller <- []
Line B — The base case must stop when the array has 0 or 1 items, so it uses <= 1. With >= 1 almost every array returns immediately and is never sorted. It should be: if length(array) <= 1 then.
Question 5
A function quickSort(array)
B pivot <- array
C smaller <- []
D larger <- []
E // partition the rest next
Line B — The pivot must be a single element, not the whole array. It should be: pivot <- array[0].

Student with a class code? Join your class →