Part 1: Fundamental Concepts of Computing

7. Common Algorithms in Action

Common Algorithms in Action

Now that you can describe steps with pseudocode and flowcharts, let's meet the classic algorithms every beginner should know. These small, reusable techniques appear again and again inside real software, so recognising them is a huge head start.

1. Linear Search — Look at Everything

The simplest way to find a value in a list is to check each item from the start until you find it (or run out). This is linear search.

INPUT list, target
FOR each item in list
    IF item == target THEN
        OUTPUT "Found!"
        STOP
    END IF
END FOR
OUTPUT "Not found"
Start at first item item =target? Output "Found!" moreitems? Output "Not found" yes no no yes (next)

2. Binary Search — The "Guess the Number" Trick

If the list is already sorted, you can do far better. Think of the game where a friend picks a number from 1 to 100 and you guess. A smart player always guesses the middle (50), learns "higher" or "lower", and throws away half the possibilities with every guess. That is binary search: check the middle, then search only the half that could contain the target. A list of 1,000,000 items needs at most about 20 guesses!

3. Finding Max / Min, Summing, and Counting

These "accumulator" patterns keep a running result as they walk through a list — you already saw the idea with "largest of three":

  • Max/Min: remember the biggest (or smallest) seen so far, update when you see a better one.
  • Sum: start a total at 0 and add each item.
  • Count: start a counter at 0 and add 1 whenever an item matches a condition.

4. Bubble Sort — Putting a List in Order

Sorting rearranges a list from smallest to largest. Bubble sort repeatedly compares neighbouring pairs and swaps them if they are out of order; large values "bubble" to the end.

INPUT list
REPEAT
    SET swapped = false
    FOR i FROM 0 TO length-2
        IF list[i] > list[i+1] THEN
            SWAP list[i] and list[i+1]
            SET swapped = true
        END IF
    END FOR
UNTIL swapped is false
OUTPUT list

Here is a trace sorting [5, 1, 4, 2]. Each row shows the list after one full pass:

PassPosition 1Position 2Position 3Position 4Note
Start5142unsorted
After pass 114255 bubbled to end
After pass 212454 settled
After pass 31245no swaps → done

Why Some Algorithms Are Faster

Two algorithms can both give the correct answer yet take wildly different amounts of work. A useful way to compare them is to ask: as the input grows, how fast does the number of steps grow? Linear search checks items one by one, so doubling the list roughly doubles the work. Binary search throws away half each time, so even a huge list needs only a handful of steps. You do not need heavy mathematics yet — just the intuition that "fewer steps as data grows" means "scales better".

List sizeLinear search (worst case)Binary search (worst case)
1010 checks~4 checks
1,0001,000 checks~10 checks
1,000,0001,000,000 checks~20 checks

This is why choosing the right algorithm matters so much: the same task can be instant or painfully slow depending on the method. Recognising these classic patterns — search, accumulate, sort — and knowing roughly how they scale is a foundation you will build on for the rest of your programming life.