Searching — Pseudocode

A linear search checks the items of a list one at a time from the front. The loop is FOR i ← 0 TO LENGTH(list) - 1, and inside it IF list[i] = target compares…

The order slips at Mrs. Sri's stall are stacked by the till, one slip per customer. This afternoon the stack has eight slips. A customer comes up and asks, "Mrs. Sri, is the order for Rina ready?"

Take a guess: how many slips does Mrs. Sri have to look at before she can answer?

The answer: it depends on where Rina's slip is.

This way of searching, checking one at a time from the front until you find it or until everything has been checked, is called a linear search. In this lesson you write it as a program.

One at a time from the front

The list from the Lists lesson is all a search needs. In this program, the list queue plays the part of the stack of slips, and target holds the name you are looking for. The loop runs from item number 0 to the last number. In each round, the item at that number is compared with target. The result is below the code:

queue ← ["Sari", "Dodi", "Rina", "Bayu", "Wati"]
target ← "Rina"
FOR i ← 0 TO LENGTH(queue) - 1
    PRINT "Check", queue[i]
    IF queue[i] = target THEN
        PRINT "Found at index", i
    END IF
END FOR
Check Sari
Check Dodi
Check Rina
Found at index 2
Check Bayu
Check Wati

Rina is at index 2, not 3. Index is the usual word for an item number, and item numbers start at 0. This program uses a plain FOR, not FOR EACH, because it also has to print the item number.

Look at the last two printed lines. Rina was found on the third check, but the program still checks Bayu and Wati. For five names that is fine. For five thousand names, many checks are wasted.

Stop as soon as it's found

In the Functions lesson, RETURN ends a function at once. If RETURN is inside a loop, that loop stops too. This function, findIndex, uses it to stop as soon as the name is found:

FUNCTION findIndex(list, target)
    FOR i ← 0 TO LENGTH(list) - 1
        PRINT "Check", list[i]
        IF list[i] = target THEN
            RETURN i
        END IF
    END FOR
    RETURN -1
END FUNCTION

queue ← ["Sari", "Dodi", "Rina", "Bayu", "Wati"]
PRINT "Rina is at index", findIndex(queue, "Rina")
Check Sari
Check Dodi
Check Rina
Rina is at index 2

Now there are only three checks, not five. As soon as list[i] = target is TRUE, RETURN i hands back that item number and leaves the function. RETURN -1 is explained in the next section.

Without a function, you can write a search that stops early with WHILE. The loop stops for one of two reasons: every item has been checked, or the name is found.

queue ← ["Sari", "Dodi", "Rina", "Bayu", "Wati"]
target ← "Rina"
i ← 0
WHILE i < LENGTH(queue) AND queue[i] ≠ target DO
    i ← i + 1
END WHILE
PRINT "Stopped at index", i
Stopped at index 2

This loop adds 1 to i as long as the name at item number i is not the target, so i stops at 2, Rina's item number.

The condition of the WHILE holds two comparisons, and their order matters. AND checks its left side first. If the left side is already FALSE, it does not check the right side. Suppose the name is not in the list: i keeps growing until it is 5. Then i < LENGTH(queue) is FALSE, so queue[5], an item that does not exist, is never asked for.

When it isn't found

In the function findIndex, RETURN -1 is written below the loop. That line is only reached when every item has been checked and none matched. An item number is never -1, so -1 is a safe sign for "not here". This program looks for Joko, who is not in the queue:

FUNCTION findIndex(list, target)
    FOR i ← 0 TO LENGTH(list) - 1
        IF list[i] = target THEN
            RETURN i
        END IF
    END FOR
    RETURN -1
END FUNCTION

queue ← ["Sari", "Dodi", "Rina", "Bayu", "Wati"]
index ← findIndex(queue, "Joko")
IF index = -1 THEN
    PRINT "Joko is not in the queue"
ELSE
    PRINT "Joko is at index", index
END IF
Joko is not in the queue

The rule is the same as for Mrs. Sri and her stack of slips. "Found" can be decided in the middle of the search. "Not here" can only be decided after every item has been checked.

Data that comes in pairs

Often you do not need the item number itself, but other data about the name you searched for, such as its order number. Keep the names and the order numbers in two lists in the same order. Then item number 2 in the list names pairs with item number 2 in the list orders:

names ← ["Sari", "Dodi", "Rina"]
orders ← [17, 4, 23]
PRINT names[2], "has order number", orders[2]
Rina has order number 23

So, to find a customer's order number, there are two steps: first find the item number of the name in the list names, then use the same item number in the list orders. That is what you do in the exercise.

In JavaScript, queue.indexOf("Rina") does the same search, and also gives -1 when the name is not found. In Python, "Rina" in queue answers whether Rina is in the list or not. Python also has queue.index("Rina"), which gives Rina's item number. But unlike indexOf, it does not give -1 when the name is not there. For example, queue.index("Joko") stops the program with the message ValueError: 'Joko' is not in list. So first check with in that the name is there. The Linear Search lesson in the Algorithms category explains this search further, including how long it takes when there are millions of items.

⚠️ Saying "not here" too soon

TL;DR: A linear search checks the items of a list one at a time from the front. The loop is FOR i ← 0 TO LENGTH(list) - 1, and inside it IF list[i] = target compares each item with target, the value you are looking for. Once the item is found, the search should stop, for example with RETURN i inside a function. "Not here" can only be decided after every item has been checked, usually with RETURN -1 below the loop. An item number is never -1, so -1 means "not here". Data that comes in pairs, such as a name and its order number, is kept in two lists in the same order. The item number found in the first list is then used in the second list.

⚡ One at a time from the front

Write:

queue ← ["Sari", "Dodi", "Rina", "Bayu", "Wati"]
target ← "Rina"
FOR i ← 0 TO LENGTH(queue) - 1
    PRINT "Check", queue[i]
    IF queue[i] = target THEN
        PRINT "Found at index", i
    END IF
END FOR
Guess first: Rina is found on the third check. Does the program stop there?

No. The program still checks Bayu and Wati, because the FOR runs to the last item number.

Meaning: This is a linear search: checking the items of a list one at a time from the front. FOR runs from item number 0 to the last number, and IF compares the item at that number with target, the name you are looking for. Rina is found at index 2, not 3: index is the usual word for an item number, and item numbers start at 0.

⚡ Stop as soon as it's found

Meaning: To make the search stop as soon as the name is found, put the loop inside a function. Inside the IF, replace the PRINT line with RETURN i. That line hands back the item number that was found and leaves the function at once, so the loop stops too. For Rina, that leaves three checks, not five.

⚡ "Not here" is decided last

Guess first: in the first program, the one that still checks Bayu and Wati, the IF gets an ELSE that prints Rina is not here. How many times is Rina is not here printed?

Four times: twice before Found at index 2, then twice more after it. An ELSE inside the loop answers for each item, not for the whole list.

Meaning: "Found" can be decided in the middle of the search. "Not here" can only be decided after every item has been checked. In a function, RETURN -1 below the loop does that: the line is only reached when no item matched. An item number is never -1, so -1 means "not here".

⚡ Data that comes in pairs

Write:

names ← ["Sari", "Dodi", "Rina"]
orders ← [17, 4, 23]
PRINT names[2], "has order number", orders[2]

Meaning: Customers' names and their order numbers are kept in two lists in the same order. Item number 2 in names pairs with item number 2 in orders, so the program prints Rina has order number 23. To find someone's order, find the item number of the name in names, then use the same number in orders. That is what you do in the exercise.

What you will learn