Learn

What Is an Algorithm?

Understand an algorithm through an original maximum-finding example, including inputs, correctness, and resource costs.

A method with a defined job

An algorithm is a specified procedure for solving a computational problem. A program is an implementation of procedures in a particular language and environment. The distinction lets us discuss a method without first choosing a laptop, programming language, or cloud provider. MIT’s models-of-computation notes make this connection between an abstract procedure and the operations available in a computing model.

A useful algorithm description identifies its inputs, required output, valid assumptions, and stopping condition. “Find the best record” is incomplete until “best” has a definition. The procedure needs something precise enough that another person can follow and check it.

Design a maximum finder

Suppose a list contains the daily counts 9, 4, 17, 12, and 6. We want the largest count. Begin by remembering the first value, 9, as the largest seen. Compare 4 with 9 and keep 9. Compare 17 with 9 and remember 17. Compare 12 and then 6 with 17. Neither is larger, so return 17.

The same procedure works for a much longer nonempty list. It does not need to sort the values first. In words: remember the first item, inspect each remaining item, replace the remembered item whenever a larger value appears, and return the remembered value after the last comparison.

Explain why it is correct

After the first item, the remembered value is the maximum of the items inspected so far. If the next value is larger, replacing the remembered value preserves that statement. If it is smaller or equal, keeping the remembered value also preserves it. Once every item has been inspected, the remembered value is the maximum of the whole list.

This reasoning is a simple invariant: a fact that remains true as the procedure progresses. It explains all valid lists under the assumptions, rather than merely showing that one example happened to succeed. A test can still catch an implementation mistake, such as accidentally replacing the value when a smaller item arrives.

Specify awkward cases

What should happen for an empty list? There is no first value to remember. The interface might reject empty input or return an explicit “no maximum” result. Returning zero would be misleading for a list containing only negative values. A procedure with an unspecified empty case is not ready to be implemented reliably.

What if two items tie for the maximum? If the output is only a numeric value, the tie changes nothing. If the output is the entire record, the procedure needs a rule: return the first, the last, or all tied records. Data types also matter. Comparing integers is different from comparing names under a language-sensitive ordering.

Count work without claiming a time

For n items, this procedure makes n minus one value comparisons. It needs one remembered maximum and an iteration position, aside from the input. These are abstract resource observations, not benchmark results. A comparison between two fixed-width numbers and a comparison between two long strings need not take the same time.

The method illustrates why clearer problem definitions can remove unnecessary work. Sorting the entire list would answer a more demanding question than finding one maximum. That extra capability might be useful later, but it should be chosen intentionally. Good algorithm design begins with the output actually required.

To evaluate an implementation, test normal lists, one item, negative values, duplicates, and the empty-input behavior. Then measure it on a declared workload. Read Big O notation for how growth is described, and research methods for how correctness and measured performance belong in a credible comparison.