Reading preferences
Concept

Thinking in Algorithms

Turn an everyday job into clear steps, then check that those steps finish and give the right answer.

algorithm, correctness, input, output, termination, edge cases

Start with an everyday job

Imagine finding the tallest book in a small pile. Pick one book as your current tallest. Compare the next book with it. If the next is taller, remember that book instead. Continue until you have checked every book.

An algorithm is a clear set of steps for doing a job. It is not necessarily computer code: you can follow these steps with real books first.

Why we need clear steps

"Find the tallest" tells us the goal, but not how to get there. Clear instructions make a job repeatable. A computer needs precise decisions such as "if this number is larger, replace the number we remembered".

An input is what the steps receive, such as [3, 7, 2, 5]. The output is the result, such as 7. Before writing the steps, decide what counts as a valid input and what to do when there is no answer.

Where algorithms are used

  • Finding the cheapest price or largest measurement.
  • Searching a contact list for a name.
  • Arranging numbers in order.
  • Following a route through a map.

These jobs need different steps. An algorithm is a method for one defined job, not a magic instruction that solves every problem.

Walk through the decisions

For a non-empty list of ordinary comparable numbers:

  1. Remember the first number as the largest so far.
  2. Look at each remaining number once.
  3. Replace the remembered number only if the new one is larger.
  4. Return the remembered number after the last item.
Number just checkedLargest rememberedWhy
33First number starts the process
777 is larger than 3
272 is not larger than 7
575 is not larger than 7

Why these steps give the right answer

After each comparison, the remembered value is the largest of everything seen so far. This is a promise we can explain after every step, called an invariant. The next comparison keeps that promise true. Once every item has been seen, the remembered value is the largest in the whole list.

Correctness means giving the specified answer for every allowed input. The procedure also needs to terminate, meaning stop: here we move to the next item and there are only finitely many items.

Python illustration

def largest_number(values):
    if not values:
        return None
    largest = values[0]
    for value in values:
        if value > largest:
            largest = value
    return largest

print(largest_number([3, 7, 2, 5]))
print(largest_number([]))

Prepared output — not live execution:

7
None

values[0] means the first item; Python counts positions from zero. for visits the items one by one. This simple code also checks the first item against itself, an unnecessary but harmless comparison. None means no result, not the number zero. The empty-list check must happen before reading the first item.

Check the difficult small cases

Try no items, one item, equal values, negative values, and a largest value at either end. Starting with zero instead of the first item would give a wrong answer for a list containing only negative numbers.

Tests help expose mistakes; examples alone do not prove a general claim. This method assumes a finite list of numbers with a sensible ordering rule and does not handle special unordered values such as NaN.

There is also a separate question: how much work and storage do these steps need? Explore time and space complexity after learning how lists and traversal work.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. Which instruction is most useful to someone following an algorithm?
2. For finding the largest number in a list, what is the input?
3. A method gives the right answer for one example. What can we conclude?
4. What should our largest-number procedure do for an empty list?

References and further reading