Memra

What an algorithm is, and why efficiency matters

◈ 4 cards

Algorithm vs problem vs instance, what "correct" means, and why a better growth rate beats faster hardware.

The vocabulary the whole course stands on

An algorithm is a well-defined computational procedure that takes some input and produces some output in a finite amount of time. Equivalently: a finite sequence of unambiguous steps that transforms input into output. The phrase finite time is doing real work — it is what separates an algorithm from an arbitrary process that might loop forever.

Three words get confused constantly; pin them down now because the exam tests the distinction:

  • A problem states what must be computed — the desired input/output relationship, in general terms. Example, the sorting problem: given a sequence , output a permutation of it with .
  • An instance is one specific input that satisfies the problem's constraints. is an instance of the sorting problem; its solution is .
  • An algorithm is how — one concrete procedure that solves the problem for every instance. Many algorithms solve the same problem, which is exactly why efficiency comparisons matter.

Notice the sorting output must be a permutation of the input (you may not invent or drop values), and (not ) is what permits duplicates.

What makes an algorithm correct

An algorithm is correct if, for every problem instance, it (1) halts in finite time, and (2) outputs the right answer. Both halves are required. An algorithm that always returns the right answer but sometimes loops forever is not correct; neither is one that always halts but sometimes lies.

Correctness comes before efficiency in the logical order: running-time analysis only means something for an algorithm that actually solves the problem. A beautiful procedure that returns wrong answers is not a fast algorithm — it is not an algorithm for that problem at all. (There is a useful exception: some incorrect algorithms are still valuable when their error rate is controllable — randomized primality tests, in Module 8, are the classic case.)

Why efficiency: a better growth rate beats faster hardware

Two algorithms for the same problem can grow at very different rates. Insertion sort takes time roughly ; merge sort takes roughly (here ). The constants depend on the machine and the code, but the functional form versus — is what dominates as grows. Because , there is always a crossover beyond which merge sort wins, no matter how much smaller is than .

Worked example — Computer A vs Computer B (the most-cited example in CLRS). Stack the deck for insertion sort:

  • Computer A: instructions/sec, hand-optimized insertion sort needing instructions.
  • Computer B: only instructions/sec (1000× slower hardware), merge sort from a sloppy compiler needing instructions (a 25× larger constant).

Sort numbers:

Computer B finishes about 17× faster — despite slower hardware, a worse compiler, and a bigger constant. At the gap explodes: insertion sort takes more than 23 days, merge sort under 4 hours. The lesson CLRS draws: algorithms are a technology. A better algorithm can overwhelm a 1000× hardware advantage, and that leverage grows with the problem size.

The one caveat: this only applies where a better algorithm exists. For the NP-complete problems in Module 9, no efficient algorithm is known — algorithmic cleverness has a boundary, and recognizing it tells you when to stop searching and reach for approximation (Module 10) instead.

instancealgorithmalgorithmSORTING problemWHAT: a sorted permutation⟨31,41,59,26,41,58⟩one legal inputinsertion sortHOW: ≈2n²merge sortHOW: ≈50n lg n
One problem, many instances, many algorithms — which is exactly why efficiency comparisons matter.
Computer AComputer Bhardware10¹⁰ instr/s10⁷ instr/salgorithminsertion sortmerge sortinstructions2n²50n lg nn = 10⁷20,000 s ≈ 5.5 h≈1,163 s < 20 minn = 10⁸> 23 days< 4 hoursB wins by ~17× at n = 10⁷ despite slower hardware and a 25× larger constant.
The CLRS Computer-A-vs-B numbers: a better growth rate overwhelms a 1000× hardware advantage, and the gap widens with n.
NORMAL ~/memra/learn/comp-372/what-is-an-algorithm utf-8 LF