Back to Challenge Set 19

Pseudocode Challenge: Tracing a Search

Difficulty  

divider

Objective

Three searches in exam notation — Activities 2.9 and 2.10. Two are binary searches, which is a recurring exam pattern: you are given a list and a target and asked what happens, with no computer in the room.

Watch the indexing. Every list on this page starts at 1. The same search in C# would give an index one lower.

Work these on paper. There is no computer on exam day.


Reference Card

What you are allowed to use
a ← expression assign the result to a
DISPLAY(expression) show it, FOLLOWED BY A SPACE
= ≠ > < ≥ ≤ comparison; = means EQUALS
aList[1] THE FIRST ELEMENT, not aList[0]
LENGTH(aList) how many elements
a MOD b remainder: 17 MOD 5 is 2
a / b ORDINARY division: 17 / 5 is 3.4
REPEAT UNTIL(condition) repeats UNTIL it becomes true,
the opposite of a while loop
IF(condition) { } ELSE { }

There is no rounding on the reference sheet, which is why the binary search below works out its middle with MOD, as Activity 2.10's exam panel showed.


The Problems

Problem 1 — Trace

What does this display? Write down every character, spaces included.

Problem 1
aList ← [8, 3, 9, 3, 5]
target ← 3
i ← 1
count ← 0
REPEAT UNTIL(i > LENGTH(aList))
{
IF(aList[i] = target)
{
DISPLAY(i)
count ← count + 1
}
i ← i + 1
}
DISPLAY(count)

Then answer this: DISPLAY puts a space after every value. Write out the exact output, and say how many characters long it is.

Problem 2 — Trace

Build a table with a row for each pass: low, high, mid, and aList[mid]. Then say what is displayed and what is returned.

Problem 2
aList ← [4, 11, 19, 26, 33, 41, 58, 70, 82]
PROCEDURE search(list, target)
{
low ← 1
high ← LENGTH(list)
REPEAT UNTIL(low > high)
{
sum ← low + high
mid ← (sum - (sum MOD 2)) / 2
DISPLAY(mid)
IF(list[mid] = target)
{
RETURN(mid)
}
IF(list[mid] < target)
{
low ← mid + 1
}
ELSE
{
high ← mid - 1
}
}
RETURN(-1)
}
result ← search(aList, 58)
DISPLAY(result)

Then answer this: the list has nine items and this search looked at very few of them. How many comparisons did it make, and what is the most it could ever make on a list of nine?

Problem 3 — Trace

The same list and the same procedure as Problem 2, called with a different target. 30 is not in the list. Build the table again: what is displayed, and what is returned?

Problem 3
result ← search(aList, 30)
DISPLAY(result)

Then answer this: the loop ended because low passed high. Write down the values they held at the end, and explain what it means for the range between them to be empty.

And this: mid is written as (sum - (sum MOD 2)) / 2 rather than (low + high) / 2. Give an example where the second one gives something that is not a valid index — and say why the same line in C# would have been fine.


Submit

  • What Problem 1 displays, written out exactly, and its length
  • Your trace tables for Problems 2 and 3, and what each displays and returns
  • Your answers to the three Then questions

This is graded on completion, not accuracy. Show your working — a wrong answer with visible reasoning is worth more to both of us than a right one with none.

Commence Challenge