Binary Search algorithm in pseudocode

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

This Binary Search algorithm in pseudocode worksheet is part of the VCE Pseudo Code journey. Print it as a booklet?

Binary Search algorithm in pseudocode

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does binary search require the array to be sorted?
A Yes
B No
2. Does binary search check every element in the array?
A Yes
B No
3. Is mid recalculated during each loop?
A Yes
B No
4. Does binary search use low and high to track the search range?
A Yes
B No
5. Is binary search generally faster than linear search on large sorted arrays?
A No
B Yes

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1low ← 1 ACalculates the middle index of the range
2mid ← (low + high) DIV 2 BSets the starting point of the search range
3Repeatedly halves the search range CWrong - the array must be sorted first
4Compares the middle element to the target DWHILE low <= high
5Binary search checks every element EWrong - it skips half the elements each time
6Binary search works on unsorted arrays FIF numbers[mid] = target THEN

Definitions

Binary Search

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
mid ← (low + high) ___ 2
2.
_____ low <= high
3.
WHILE low __ high
4.
__ numbers[mid] = target THEN
5.
_______ "Found at position", mid

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Works on unsorted arrays
B Compares middle element
C Halves the search space
Question 2
A Data must be sorted first
B Checks every element always
C Uses low, high and mid
Question 3
A Uses low and high pointers
B mid is the halfway point
C mid never changes value
Question 4
A WHILE low <= high
B mid ← (low + high) DIV 2
C IF numbers[mid] = target
D Loop runs forever always
Question 5
A low starts at 1
B high starts at array size
C mid is halfway between them
D IF numbers[mid] = target THEN
E Search space grows each loop

Answer Key

Binary Search algorithm in pseudocode
Section A: Quick Questions
Q1
A — Yes
Q2
B — No
Q3
A — Yes
Q4
A — Yes
Q5
B — Yes
Section B: Match the Pairs
Q1
low ← 1 → Sets the starting point of the search range
Q2
mid ← (low + high) DIV 2 → Calculates the middle index of the range
Q3
Repeatedly halves the search range → WHILE low <= high
Q4
Compares the middle element to the target → IF numbers[mid] = target THEN
Q5
Binary search checks every element → Wrong - it skips half the elements each time
Q6
Binary search works on unsorted arrays → Wrong - the array must be sorted first
Section C: Definitions
Q1
Binary Search — An algorithm that repeatedly halves a sorted array to quickly find a target value.
Section D: Fill in the Blanks
Q1
mid ← (low + high) DIV 2
Q2
WHILE low <= high
Q3
WHILE low <= high
Q4
IF numbers[mid] = target THEN
Q5
DISPLAY "Found at position", mid
Section E: Spot the Error
Q1
A — Binary search needs sorted data, not unsorted.
Q2
B — That's linear search - binary search skips elements.
Q3
C — mid is recalculated each loop, so it does change.
Q4
D — The loop stops once low is greater than high.
Q5
E — The search space shrinks each loop, not grows.