Courses / Computer Science I
Algorithmics

Complexity Analysis

Computer Science I 292 words Free to read

Measuring Cost Precisely

Complexity analysis determines how an algorithm's resource use — time (operations) and space (memory) — grows with input size nn, so you can compare algorithms and predict scaling without running them.

The tool is asymptotic notation, which describes growth for large nn, ignoring constants and lower-order terms:

To analyze, count the dominant operations as a function of nn: a single loop over the data is O(n)O(n); a loop inside a loop is O(n2)O(n^2); halving each step is O(logn)O(\log n); and constant work regardless of nn is O(1)O(1). Only the fastest-growing term matters: O(n2+n)O(n^2 + n) is just O(n2)O(n^2), because for large nn the n2n^2 term dominates.

Costs can differ by case. The worst case (the input that makes the algorithm slowest) is the usual guarantee — it bounds performance no matter what. The average case describes typical behavior over expected inputs (quicksort is O(nlogn)O(n \log n) average but O(n2)O(n^2) worst). The best case is often uninformative.

Do not forget space complexity: an algorithm may be fast but memory-hungry. Merge sort is O(nlogn)O(n \log n) time but needs O(n)O(n) extra space to merge, while heapsort sorts in place with O(1)O(1) extra — another space–time trade-off to weigh.

Common pitfall: keeping constant factors and lower-order terms in a Big-O answer, writing "O(3n2+5n+7)O(3n^2 + 5n + 7)." Asymptotic analysis drops constants and lower-order terms: that is simply O(n2)O(n^2). The whole point is to compare growth shape, not exact operation counts — the dominant term alone captures how the algorithm scales.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

10practice questions
2interactive scenes
Start Computer Science I free

Algorithmics