MODULE 18

Algorithms & Complexity

Big-O Growth Comparison

Bubble Sort (step trace)

Binary Search (step trace)

Understanding Algorithms & Complexity

Algorithmic complexity measures how an algorithm's running time or memory use grows as its input grows, using Big-O notation to abstract away hardware-specific constants and focus on the trend that actually matters at scale. Knowing an algorithm's complexity class lets you predict whether it will finish in milliseconds or centuries before ever running it on real, large-scale data.

Key Definitions & Formulas

  • O(1) — constant time: runtime doesn't depend on input size (e.g. array index lookup).
  • O(log n) — logarithmic: runtime grows very slowly (e.g. binary search).
  • O(n) — linear: runtime grows proportionally with input size.
  • O(n log n): typical of efficient sorting algorithms like merge sort.
  • O(n²) — quadratic: typical of simple nested-loop algorithms like bubble sort.
  • O(2n) — exponential: typical of brute-force search over all subsets; becomes infeasible quickly.

Worked Example

Searching an unsorted list of a million items one by one takes up to a million comparisons (O(n)). Sorting it once with an O(n log n) algorithm costs about 20 million comparisons, but then every future search can use binary search at O(log n) — about 20 comparisons — a trade worth making if you'll search the same data many times.

Where This Is Used

  • Choosing the right algorithm and data structure for large-scale software systems.
  • Estimating server costs and scalability limits before deployment.
  • Security: many cryptographic schemes rely on certain problems being computationally infeasible (exponential time).
  • Technical interviews and competitive programming, where complexity analysis is a core skill.