Algorithmic Efficiency

Problems and instances

A problem is a general description of a task that can, or cannot, be solved algorithmically.
An instance of a problem is that same task with specific input attached. Searching a list is a problem. Searching the list [35, 62, 18, 47] for the value 18 is an instance of it.
The distinction matters because efficiency is never a statement about one instance. It is a statement about how the cost behaves as the instances get bigger.
Kind of problemWhat it asks forExample
Decision problema yes or no answercan this jigsaw be finished with the pieces in the box?
Optimization problemthe best answer out of manywhat is the fastest order to place the pieces?

Efficiency

Efficiency is an estimate of the amount of computational resources an algorithm uses.
It is written as a function of the size of the input, and that size is almost always called n. This is why the answers below look like 2n rather than like a fixed number of seconds. An algorithm does not have one running time. It has a running time that depends on how much you feed it.
Efficiency is often quoted for the worst case, the input that makes the algorithm do the most work, and sometimes for the average case. It is determined through formal or mathematical reasoning about how the work grows with n. Informally, you measure it by counting how many times a statement, or a group of statements, executes.
Two different algorithms can both be correct for the same problem and still have wildly different efficiencies. A sorting algorithm that needs more comparisons is slower than one that needs fewer, and both of them still sort.
Correctness is a claim about what comes back. Efficiency is a claim about what it cost to get there. Neither one settles the other.

Counting statements

In each example below, count how many times a statement inside a loop body executes, and let n be the number of elements in readings.
The initialization above the loop runs once whatever the list holds, so it does not grow with n and is left out of the count.
How many times the statements run when n = 6. One loop after another: two rows of 6 for loop 1, line 1 and loop 1, line 2, and one row of 6 for loop 2; 2n + n = 3n = 18 runs. One loop inside the other: a 6 by 6 grid, 6 outer passes, 6 inner each; n × n = n squared = 36 runs. Double n to 12: 3n becomes 36, n squared becomes 144.
The program below counts how many times one statement runs, in loops placed one after the other and in loops nested one inside the other. Run it, then change it and run it again:
• Change n to 20.
• Add a third loop inside the nested pair: put for (let k = 0; k < n; k++) { on a new line just above nested += 1;, and a closing } on a new line just below it.
let n = 10;

let oneAfterOther = 0;
for (let i = 0; i < n; i++) {
  oneAfterOther += 1;
}
for (let i = 0; i < n; i++) {
  oneAfterOther += 1;
}

let nested = 0;
for (let i = 0; i < n; i++) {
  for (let j = 0; j < n; j++) {
    nested += 1;
  }
}

console.log("n is", n);
console.log("Loops one after the other:", oneAfterOther);
console.log("One loop inside the other:", nested);
Output
Press Run to see what this program prints.

Note: This IDE is for experimenting with the code in this lesson. Your changes are not saved, and refreshing the page puts back the original lesson code.

Reasonable and unreasonable running times

Algorithms with a polynomial efficiency, meaning constant, linear, quadratic, cubic and so on, are said to run in a reasonable amount of time. The label describes how the work grows with the input. A polynomial algorithm can still take a long time on a very large input, but its growth is far gentler than exponential growth.
But there are important, practical problems for which nobody knows a polynomial time algorithm. Algorithms with exponential or factorial efficiencies run in an unreasonable amount of time. A faster machine does not rescue them. On a 2 to the n algorithm, a computer a thousand times faster handles only about ten more items.
Chart: steps as the input grows, for n, the size of the input, from 0 to 12, with steps from 0 to 500. n rises slowly to 12. n squared curves up to 144. 2 to the n is off the chart by n = 9; at n = 12 it is 4,096.