Skip to content

Data Structure and Algorithms

Updated: at 10:22 AM

Time Complexity and Space Complexity

Part 1: Time and Space Complexity

What is Time Complexity?

Time complexity measures how much time an algorithm takes as the input size grows. We usually describe it in terms of n, where n is the size of the input.

Example:

Common Time Complexities

ComplexityNameExample
O(1)ConstantAccessing an element by index in an array
O(log n)LogarithmicBinary search
O(n)LinearSimple loop through an array
O(n log n)LinearithmicEfficient sorting algorithms (e.g., Merge Sort)
O(n²)QuadraticNested loops
O(2ⁿ)ExponentialRecursive Fibonacci without memoization

What is Space Complexity?

Space complexity measures how much extra memory an algorithm uses as the input grows.

Example:

Part 2: Asymptotic Notations

These notations describe how an algorithm behaves as n becomes large.

Big O Notation (O) — Worst Case

Describes the upper bound of an algorithm’s running time.

Example: In linear search, worst case is O(n).

Omega Notation (Ω) — Best Case

Describes the lower bound (best performance possible).

Example: In linear search, if the element is the first one, best case is Ω(1).

Theta Notation (Θ) — Tight Bound

Describes the exact bound (when best and worst cases are similar).

Example: If an algorithm always runs in n log n time, it’s Θ(n log n).