Tutorial 2: The Efficiency of Algorithms and Computing Procedures

Unit 2 ยท Section 2

Objectives

Efficiency asks how resource use grows with input size. Big-O describes an upper growth category while ignoring constants. A single pass is often $O(n)$; nested independent scans are often $O(n^2)$; binary search on sorted data is $O(\log n)$.

linear search: 1, 2, ... n comparisons
binary search: halve the remaining range each step

Exercises

  1. Count comparisons in a linear search.
  2. Classify three loops by growth.
  3. Describe a memory-for-time trade-off.

Self-check

  1. What does n represent?
  2. Why ignore constants?
  3. When is extra memory useful?

Self-Check Quiz

1. Which grows more slowly: $O(n)$ or $O(n^2)$?

Answer$O(n)$ grows more slowly for sufficiently large input.

2. Does Big-O give exact runtime?

AnswerNo. It describes growth, not machine-specific exact time.

Homework

  1. Compare two algorithms for duplicate detection.
  2. Estimate time and space growth.
  3. Recommend one under a memory constraint.
Sample answerNested comparison is $O(n^2)$ time and constant extra space; a set-based method is typically $O(n)$ expected time and $O(n)$ space. Choose the set when memory is available and input is large.