Form 3 Asas Sains Komputer (ASK) asks you to compare two ways of searching a list and to find and correct a logic error in a short program. Both skills come from counting steps and testing with chosen inputs, and this page shows each on small original examples.
It continues the Form 2 guide to tracing and testing and belongs to pupils whose school offers ASK in Form 3 under KSSM. Sains Komputer in Form 4 is a separate subject, which the page on moving from lower-secondary computing to Form 4 Computer Science explains.
How do two search traces differ?
Take the sorted list 2, 5, 7, 9, 12, 15, 18 in positions 1 to 7, and search for 15. Linear search checks position 1, 2, 3 and so on until it finds a match. It compares with 2, 5, 7, 9, 12 and 15, so it needs 6 comparisons.
Binary search checks the middle position first. The middle of positions 1 to 7 is position 4, which holds 9. Since 15 is larger, it keeps only positions 5 to 7, whose middle is position 6, and finds 15 there after 2 comparisons.
| method | positions checked | comparisons |
|---|---|---|
| linear | 1, 2, 3, 4, 5, 6 | 6 |
| binary | 4, 6 | 2 |
The explanation of the difference is that linear search discards one item per step while binary search discards half the remaining items per step.
What is the common mistake with binary search?
A common mistake is to use binary search on the list 9, 2, 15, 5, 18, 7, 12 and trust the answer. The middle is 5, so the method moves right, then checks 7, then 12, and reports that 15 is not found. That is wrong because 15 sits in position 3, and the first step threw that half away.
Binary search is only safe on a sorted list.
How do I trace and correct a logic error?
This original program should count how many of four marks are 50 or more.
count ← 0
FOR i ← 1 TO 4
INPUT mark
IF mark > 50 THEN
count ← count + 1
END IF
NEXT i
PRINT count
Trace the marks 40, 50, 65 and 50. Only 65 is above 50, so the program prints 1, but the intended answer is 3. The error is on the IF line, where > should be >= .
Record the test as a table: input 40, 50, 65, 50; expected 3; before the fix 1; after the fix 3. A test using only 40 and 65 would give 1 both before and after, so it would not show the fix working.
Where should I go next?
Try the eight questions in Form 3 Asas Sains Komputer practice, which use new lists and new programs. The Form 3 overview lists your other subjects.
Computer room tasks stay with the school. These pages train the tracing and testing habits that those tasks use.
When two algorithms both give the answer and you cannot say why one takes fewer steps, a lower secondary Asas Sains Komputer teacher can count the comparisons with you on a small list until the difference is obvious.