DSA-VerseLearn by watching code run
DashboardData StructuresAlgorithmsVisualizerCORECoding PlaygroundInterview QuestionsComplexity AnalyzerLearning NotesBookmarksLearning ProgressSettings

Time & Space Complexity Analyzer

Compare growth rates, study curated best/average/worst-case complexity, and see real operation counts measured from instrumented implementations — with every limitation stated clearly.

Exact complexity cannot be determined just by running code. This page separates three different kinds of evidence:

  • Theoretical — Theoretical complexity is curated, human-verified metadata describing how cost grows as input size n → ∞. It ignores constants and lower-order terms.
  • Measured — Measured values are exact operation counts recorded while the instrumented algorithm ran on this specific input. They show real behaviour for one n, but cannot by themselves prove an asymptotic bound.
  • Heuristic — Heuristic analysis inspects code structure (loop nesting, halving, recursion). It is approximate and can be wrong — e.g. it cannot see that a loop bound is constant or that recursion is memoised.

Growth-rate comparison

Theoretical number of basic operations f(n) for each complexity class. Adjust n to see how quickly they diverge.

Operation counts at n = 16
ClassNameOperations at n = 16at 2n = 32Typical example
O(1)Constant11Array index access, stack push/pop
O(log n)Logarithmic45Binary search
O(n)Linear1632Linear search, BFS/DFS (V + E)
O(n log n)Linearithmic64160Merge sort, heap sort
O(n²)Quadratic2561,024Bubble, selection, insertion sort
O(2ⁿ)Exponential66K4.3BNaive recursive Fibonacci, all subsets
O(n!)Factorial20.9T2.6e35All permutations, brute-force TSP

Algorithm analysis

Curated theory for the selected algorithm, compared with operation counts measured from its instrumented implementation.

Theoretical · curated
Time
Best caseO(n)
Average caseO(n²)
Worst caseO(n²)
Space
AuxiliaryO(1)
InputO(n)
TotalO(n)

Time: In the worst case (reverse-sorted input) pass i makes n−1−i comparisons, giving (n−1)+(n−2)+…+1 = n(n−1)/2 comparisons → O(n²). With the swapped flag, an already-sorted array finishes after one pass of n−1 comparisons → O(n) best case.

Space: Sorting happens in place by swapping inside the same array. Only a few index variables (i, j, swapped) are used, so auxiliary space is O(1). The input array itself occupies O(n).

Measured operation countsEmpirical

Counts come from the instrumented implementation (comparisons) at n = 8, 16, 32, 64, 128, 256. The dashed line is the theoretical growth function scaled to the largest measurement — a visual guide, not a proof.

Visualize Bubble Sort

Static analysis (heuristic)

Paste code to get a rough estimate from its structure. Always verify by reasoning about the loop bounds yourself.

Estimated time complexityLow confidence

O(n²)

  • Deepest loop nesting: 2 level(s).

Limitation: the analyzer only sees code structure. It cannot tell that a loop runs a constant number of times, that a recursion is memoised effectively, or what a library call costs internally.