Interactive Visualizer

Big O Notation

See how algorithms scale. Drag the slider and watch the curves diverge.

Complexity Growth Visualizer

n = 20

Code Examples by Complexity

O(1) Array Index Access / HashMap Lookup

Same time regardless of input size. Direct memory address calculation.

// O(1) — constant time int first = arr[0]; String val = map.get("key"); // Doesn't matter if array has 10 or 10 million elements
O(log n) Binary Search

Cut the problem in half with every step. 1 million elements? Only ~20 steps.

// O(log n) — binary search int lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) lo = mid + 1; else hi = mid - 1; } return -1;
O(n) Linear Search / Find Maximum

Touch every element exactly once. Double the input, double the time.

// O(n) — linear scan int max = arr[0]; for (int x : arr) { if (x > max) max = x; } return max;
O(n log n) Merge Sort / Efficient Sorting

The best you can do for comparison-based sorting. Divide and conquer.

// O(n log n) — merge sort void mergeSort(int[] arr, int l, int r) { if (l >= r) return; int mid = (l + r) / 2; mergeSort(arr, l, mid); mergeSort(arr, mid + 1, r); merge(arr, l, mid, r); } // Also: Arrays.sort(arr); uses TimSort
O(n²) Brute Force Two Sum / Bubble Sort

Nested loops checking every pair. Fails on large inputs — 100k elements = 10 billion ops.

// O(n^2) — brute force two sum for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] + arr[j] == target) return new int[]{i, j}; } } // Better: use HashMap for O(n) solution
O(2ⁿ) Recursive Fibonacci (no memoization)

Each call branches into two more. n=40 takes seconds. n=50 takes minutes. n=100? Heat death of the universe.

// O(2^n) — naive recursive fibonacci int fib(int n) { if (n <= 1) return n; return fib(n - 1) + fib(n - 2); } // Fix: add memoization for O(n)

Quick Reference

Complexity Name Example n=1M ops
O(1) Constant Array access, hash lookup 1
O(log n) Logarithmic Binary search 20
O(n) Linear Single loop, linear search 1,000,000
O(n log n) Linearithmic Merge sort, heap sort 20,000,000
O(n²) Quadratic Nested loops, bubble sort 1,000,000,000,000
O(2ⁿ) Exponential Recursive subsets, naive fib ... don't even try