Skip to content
SPM Tuition
Guide · Asas Sains Komputer

Form 3 Asas Sains Komputer guide

Two algorithms both find the answer and you cannot explain why one needs fewer steps.

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.

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.

Common questions

Why does binary search need a sorted list?

Binary search keeps the half of the list where the target must be, and that only holds when the items are in order. On an unsorted list it can discard the half that holds the target and report not found even though the item is there.

Is binary search always faster than linear search?

No. It needs fewer comparisons on average, but a target near the start of a list can be found by linear search just as fast. Always count the actual comparisons for the list you are given.

How do I record a test that proves a fix?

Write the input, the output you expect, the output before the fix and the output after the fix. A good test fails before and passes after. A test that gives the same answer both times proves nothing about the fix.

Does this page cover the whole Form 3 Asas Sains Komputer course?

No. It teaches two skills with original programs. Your school's scheme of work decides which topics your class covers and in what order, so follow it for lessons and use this page to practise the skills.

Want to work through this topic with a teacher? We can explain the steps, look at your attempted questions and help you decide what to practise next.

If you have a question or a piece of working, you can share it.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.