androidengineers.Book a session

Algorithm Foundations & Complexity

Common Complexity Classes (O(1), O(n), O(n log n), O(2^n))

article20 minEasy

Recognize common growth patterns

Constant-time indexing, a linear scan, comparison sorting, and subset enumeration have different scaling behavior. Recognizing the structure helps choose an algorithm before writing it.

PatternTypical growthWhy
One array accessO(1)One fixed-index lookup
Visit every elementO(n)One operation per element
Halve a search intervalO(log n)Few reductions reach size one
Merge-sort levelsO(n log n)Linear work at each logarithmic level
Enumerate all subsetsO(2ⁿ)Each item is included or excluded

For 20 elements, there are 1,048,576 subsets. For 30, there are 1,073,741,824. Faster hardware cannot turn unrestricted exponential enumeration into a scalable general solution.

A nested loop is not always quadratic: if the inner loop halves its bound, the analysis differs. Likewise, recursive syntax does not by itself imply exponential work.

Exercise

Classify scanning a matrix with r rows and c columns, sorting n records, and producing every pair of distinct records. Use both variables for the matrix rather than assuming it is square.

Check: an output containing all pairs already has quadratic size, so no algorithm can explicitly produce it in linear time.

Further reading: Growth rates

YOUR LEARNING JOURNEY

0 of 55 available lessons completed

Progress saved in this browser. No account needed.
Common Complexity Classes (O(1), O(n), O(n log n), O(2^n)) | Algorithms | Android Engineers