C++ · Module 18 · Lesson 12 min read

Big-O & Complexity Analysis

What Big-O measures

How the running time (or memory) grows as the input size n grows — ignoring constants and smaller terms. It answers: "if the data doubles, what happens to the time?"

Common complexities (fastest → slowest)

Big-ONameExamplen = 1,000,000
O(1)constantv[i], hash lookup1 step
O(log n)logarithmicbinary search, std::map find~20
O(n)linearloop through a vector10⁶
O(n log n)linearithmicstd::sort, merge sort~2 × 10⁷
O(n²)quadraticnested loops over all pairs10¹² ❌
O(2ⁿ)exponentialnaive Fibonacci, all subsetsimpossible
O(n!)factorialall permutationsimpossible

A modern CPU does roughly 10⁸–10⁹ simple steps per second.

Rules for working it out

  • Sequential steps add: O(n) + O(n) = O(n)
  • Nested loops multiply: O(n) inside O(n) = O(n²)
  • Drop constants: O(3n + 5) = O(n)
  • Keep the dominant term: O(n² + n) = O(n²)
  • Halving each step → O(log n)
  • Recursion: count calls × work per call

Best, average, worst case

Quicksort: average O(n log n), worst O(n²). Hash table: average O(1), worst O(n). Usually we care about worst or average.

Space complexity

Extra memory used: in-place reverse O(1), copying into a new vector O(n), recursion depth uses stack space.

Amortized complexity

vector::push_back is occasionally O(n) (reallocation) but O(1) on average over many pushes.

Using it in practice ✅

Before writing code, estimate: n ≤ 10⁴ → O(n²) is fine; n ≈ 10⁶ → need O(n log n) or better; n ≈ 10⁹ → O(n) streaming or O(log n).

Example

C++
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>

long long steps = 0;

int linear_search(const std::vector<int>& v, int target) {        // O(n)
    for (std::size_t i = 0; i < v.size(); ++i) { ++steps; if (v[i] == target) return static_cast<int>(i); }
    return -1;
}

int binary_search(const std::vector<int>& v, int target) {        // O(log n)
    int lo = 0, hi = static_cast<int>(v.size()) - 1;
    while (lo <= hi) {
        ++steps;
        int mid = lo + (hi - lo) / 2;
        if (v[mid] == target) return mid;
        if (v[mid] < target) lo = mid + 1; else hi = mid - 1;
    }
    return -1;
}

long long count_pairs(int n) {                                    // O(n²)
    long long c = 0;
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j) ++c;
    return c;
}

int main() {
    std::cout << std::setw(10) << "n" << std::setw(12) << "linear" << std::setw(10) << "binary" << std::setw(14) << "pairs (n^2)" << '\n';
    for (int n : { 1'000, 10'000, 100'000, 1'000'000 }) {
        std::vector<int> v(n);
        for (int i = 0; i < n; ++i) v[i] = i * 2;                  // sorted
        int target = v.back();                                    // worst case for linear search

        steps = 0; linear_search(v, target); long long lin = steps;
        steps = 0; binary_search(v, target); long long bin = steps;
        std::cout << std::setw(10) << n << std::setw(12) << lin << std::setw(10) << bin
                  << std::setw(14) << (n <= 10'000 ? std::to_string(count_pairs(n)) : "too many") << '\n';
    }
    std::cout << "Doubling n doubles linear steps but adds only ~1 binary step.\n";
    return 0;
}

/* Output:
         n      linear    binary   pairs (n^2)
      1000        1000        10        499500
     10000       10000        14      49995000
    100000      100000        17      too many
   1000000     1000000        20      too many
Doubling n doubles linear steps but adds only ~1 binary step.
*/