DAA Study Hub

1 · What an algorithm is & counting its steps

What makes something an algorithm, and how to count the steps it takes.

In one sentence

An algorithm is a precise, finite recipe that turns an input into an output. We judge it by counting how many basic steps it takes as the input size nn grows, written T(n)T(n).

Everyday analogy

A recipe. A good recipe:

  • lists its ingredients (input) and tells you what dish you'll get (output);
  • has steps that are clear ("add 5 g salt", not "add some salt"): definiteness;
  • ends at some point, not "stir forever": finiteness;
  • only asks for things a cook can actually do: effectiveness.

Two cooks with different stoves (Java vs. C, fast vs. slow laptop) follow the same recipe. That's why we analyse the recipe itself, not one cook's stopwatch time.

Key ideas

IdeaMeaning
5 propertiesinput, output, definiteness, finiteness, effectiveness
Input size nnhow "big" the input is (array length, number of digits, …)
T(n)T(n)number of basic steps on an input of size nn
Cost × frequencytotal time = Σ (cost of a line × how many times it runs)
Best / worst / average casefewest / most / typical steps over all inputs of size nn
Why not just time it?stopwatch results depend on machine, language and the inputs you happened to try

Pseudocode conventions used in this course: ← means assignment, = means equality test, arrays start at index 1 (A[1..n]), and loops are written for i ← 1 to n do.


Counting steps for CountNegatives (our own example)

CountNegatives(A, n)
1  count ← 0
2  for i ← 1 to n do
3      if A[i] < 0 then
4          count ← count + 1
5  return count
LineCostTimes it runsWhy
1c1c_11once at the start
2c2c_2n+1n + 1nn successful checks + 1 failing check that ends the loop
3c3c_3nnonce per element
4c4c_4between 00 and nndepends on how many negatives
5c5c_51
T(n)=c1+c2(n+1)+c3n+c4⋅(#negatives)+c5T(n) = c_1 + c_2(n+1) + c_3 n + c_4\cdot(\#\text{negatives}) + c_5
  • Best case (no negatives): T(n)=(c2+c3)n+(c1+c2+c5)T(n) = (c_2 + c_3)n + (c_1 + c_2 + c_5), which is linear.
  • Worst case (all negative): T(n)=(c2+c3+c4)n+(… )T(n) = (c_2 + c_3 + c_4)n + (\dots), still linear.

The pipeline from problem to program


Common mistakes

  • Counting a loop header as nn instead of n+1n + 1.
  • Saying "best case = small input". Best/worst case are about inputs of the same size nn.
  • Treating a library call like string concatenation or copy() as one step. It can cost Θ(n)\Theta(n) itself.

Mastery check

Solve without hints, then explain

For FindFirstZero(A, n), which scans left to right and stops at the first 0:

(a) Write its pseudocode. (b) How many comparisons does it make in the best and worst case? (c) Give an input that causes each.

Your mastery check
Not started
Solve the problem above with no hints, close the page and explain it out loud. Tick each step honestly.
Mastery checklist

Status

Tick all six steps to unlock “Mastered”.

Which study mode worked for you?

Saved in this browser onlySee all my progress →

References

  • 01-02 Introduction: Slides 9–11 (the 5 criteria, language independence), 27–29 (problem-solving pipeline), 30 (design techniques).
  • 03 Algorithm Analysis: Slides 9–14 (experimental vs. theoretical analysis), 15–18 (cost × frequency).
  • 04 Correctness & Space: Slides 3–5 (pseudocode notation).
  • CLRS 4e Ch 1 (role of algorithms), §2.2 (analysing algorithms).

On this page