Worksheet 01 Q
Worksheet 01 Q
10 9 8
Copy and paste the program code into the evidence document.
Task – 01 b
www.mahmoudmoussa.com 1
Mr. Mahmoud Moussa A2 CS 9618
Task – 01 c
Copy and paste the program code into the evidence document.
Task – 01 d
www.mahmoudmoussa.com 2
Mr. Mahmoud Moussa A2 CS 9618
Task – 02 a
Study the algorithm, written in pseudocode, for a binary search. The data being searched is stored in
the array SearchData[63]. The item of data being searched is stored in the variable
SearchItem.
X ← 0
Low ← 1
High ← 63
WHILE (High >= Low) AND X = 0
Middle ← INT((High + Low)/2)
IF SearchData[Middle] = SearchItem THEN
X ← Middle
ELSE
IF SearchData[Middle] < SearchItem THEN
Low ← Middle + 1
ELSE
IF SearchData[Middle] > SearchItem THEN
High ← Middle - 1
ENDIF
ENDIF
ENDIF
ENDWHILE
The binary search only works if the data in the array being searched is:
ordered / in order
The maximum number of comparisons that are required to find an item which is present in the array
SearchData is:
6 According to the pseudocode given
At the end of the algorithm, the variable X contains:
Test your program with one value that is in the array and one value that is not in the array.
Assume the array contains [100,200,300,400]
www.mahmoudmoussa.com 3