Two students may write different algorithms for the same problem. Both may give correct answers, yet one may need far more work than the other. Algorithm analysis provides the means to judge algorithms on two separate questions: is it right, and how much does it cost?

Correctness

Definition: Correctness. The property of an algorithm that it produces an acceptable output for every valid input.

A few successful examples are useful checks, but they do not prove correctness. An algorithm must work for all valid instances, including the unusual ones. To test an algorithm that finds a maximum, the following instances should be tried: a list with one value, a list where all values are equal, a list whose largest value is first, and a list whose largest value is last.

Efficiency

Definition: Efficiency. A measure of the resources an algorithm uses, chiefly the time it takes and the memory it needs.

Efficiency is described in terms of the size of the input, usually written n. The question asked is how the amount of work grows as n grows. The number of seconds is not used, because that depends on the computer. Instead, the number of basic operations, such as comparisons, is counted.

Checking each item of a list once needs about n operations. Comparing every item with every other item needs about n × n operations. Table 1.6 shows how quickly these two amounts of work move apart.

Table 1.6: Growth of work with input size

Input size n About n operations About n × n operations
10 10 100
100 100 10,000
1,000 1,000 10,00,000
10,000 10,000 10,00,00,000

Time efficiency refers to the number of operations performed. Space efficiency refers to the amount of memory used. An algorithm may be improved in one at some cost in the other.

Keeping the Two Questions Separate

Correctness and efficiency are different questions and must be asked in that order. A fast method that gives wrong answers for some valid inputs is not a solution at all.

Example 1.6: Comparing two methods for a maximum

Problem: Two methods are proposed for finding the largest value in 4, 9, 2, 15, 7. Method A looks only at the first three values. Method B scans the whole list, keeping the largest value seen so far. Judge them.

Solution:

  1. Method A examines 4, 9, 2 and reports 9. It uses 2 comparisons.
  2. Method B examines all five values. The largest so far changes from 4 to 9 to 15. It uses 4 comparisons and reports 15.
  3. The true largest value is 15, so Method A is incorrect for this instance.
  4. Method B uses n − 1 comparisons for a list of n values, and it is correct for every non-empty list.

Result: Method A is faster but incorrect, so it is rejected. Method B is correct and its work grows in proportion to n.

Common mistakes

  • Believing that an algorithm is correct because it worked for two or three examples.
  • Measuring efficiency in seconds on one computer and treating the figure as a property of the algorithm.
  • Trying to make an algorithm faster before confirming that it is correct.

Key takeaways

  • Correctness means an acceptable output for every valid input.
  • Efficiency measures time and memory in terms of the input size n.
  • Work is counted in basic operations, not in seconds.
  • Correctness is established first; efficiency is compared only among correct methods.

Practice questions

  1. Which is checked first, correctness or efficiency?
  2. Why is running time in seconds a poor measure of an algorithm?
  3. How many comparisons does a full scan need to find the largest of n values?

Answers to the practice questions

  1. Correctness. Efficiency is compared only among methods that are already correct.
  2. It depends on the computer and the program, not only on the algorithm.
  3. n − 1 comparisons.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit I