Time Complexity.

How algorithm performance scales with input size, expressed in Big O notation.

Time Complexity

Measures how running time increases with input size. Helps predict scalability for large datasets.

Space Complexity

Measures memory usage relative to input size. Critical for memory-constrained environments.

Growth Comparison

O(1) — Constant
O(log n) — Logarithmic
O(n) — Linear
O(n log n) — Log-linear
O(n²) — Quadratic

Complexity Classes

O(1)Constant Time

Operations that take the same amount of time regardless of input size.

  • Array access by index
  • Push/pop on a stack
  • Hash table insert

Hash table lookups are typically O(1), making them ideal for frequent access operations.

O(log n)Logarithmic Time

Runtime grows logarithmically. Common in algorithms that halve the problem each step.

  • Binary search
  • Balanced BST lookup
  • Divide and conquer

Database B-tree indexes use O(log n) lookups, enabling fast searches across millions of rows.

O(n)Linear Time

Runtime grows linearly. Each input element requires exactly one pass.

  • Linear search
  • Array traversal
  • Finding min/max in unsorted list

Processing a data stream is at minimum O(n) since every element must be examined.

O(n log n)Linearithmic Time

Common in efficient sorting algorithms that combine linear and logarithmic passes.

  • Merge sort
  • Heap sort
  • Quick sort (average)

Most production sort implementations (including JavaScript's Array.sort) use this class.

O(n²)Quadratic Time

Runtime grows with the square of input. Typically caused by nested loops over the data.

  • Bubble sort
  • Selection sort
  • Checking all pairs in an array

A million-item input would require a trillion operations. Avoid for large datasets.

O(n!)Factorial Time

Runtime grows with the factorial of input. Impractical beyond very small inputs.

  • Brute-force traveling salesman
  • All permutations
  • N-queens brute force

With just 20 items, a factorial algorithm would take longer than the age of the universe.

Algorithm Reference

AlgorithmCategoryBestAverageWorstSpace
Bubble SortSortingO(n)O(n²)O(n²)O(1)
Selection SortSortingO(n²)O(n²)O(n²)O(1)
Insertion SortSortingO(n)O(n²)O(n²)O(1)
Merge SortSortingO(n log n)O(n log n)O(n log n)O(n)
Quick SortSortingO(n log n)O(n log n)O(n²)O(log n)
Heap SortSortingO(n log n)O(n log n)O(n log n)O(1)
Counting SortSortingO(n+k)O(n+k)O(n+k)O(n+k)
Radix SortSortingO(nk)O(nk)O(nk)O(n+k)
Linear SearchSearchingO(1)O(n)O(n)O(1)
Binary SearchSearchingO(1)O(log n)O(log n)O(1)
Jump SearchSearchingO(1)O(√n)O(√n)O(1)
InterpolationSearchingO(1)O(log log n)O(n)O(1)
BFSGraphO(V+E)O(V+E)O(V+E)O(V)
DFSGraphO(V+E)O(V+E)O(V+E)O(V)
Dijkstra'sGraphO((V+E)log V)O((V+E)log V)O((V+E)log V)O(V)
Bellman-FordGraphO(VE)O(VE)O(VE)O(V)
Floyd-WarshallGraphO(V³)O(V³)O(V³)O(V²)
Prim'sGraphO(E log V)O(E log V)O(E log V)O(V+E)
Kruskal'sGraphO(E log E)O(E log E)O(E log E)O(V+E)
Array AccessData StructureO(1)O(1)O(1)O(1)
Array SearchData StructureO(1)O(n)O(n)O(1)
Array InsertionData StructureO(1)O(n)O(n)O(1)
Hash Table SearchData StructureO(1)O(1)O(n)O(n)
BST SearchData StructureO(1)O(log n)O(n)O(n)
AVL Tree OpsData StructureO(log n)O(log n)O(log n)O(n)

V = vertices, E = edges, k = range of values