DSA Fundamentals
An algorithm transforms an input according to rules. A data structure organizes the values those rules need. Begin by counting one operation while the input grows.
Picture the idea
To find a name in an unsorted attendance register you might read every row. In an alphabetized register, one comparison can rule out half the pages.
When to use it
Before choosing an implementation, name the input size and the repeated work.
How it works
Write the work for one iteration. Add those costs for consecutive loops and multiply only when each inner loop repeats independently for every outer iteration.
- Count comparisons in a scan of n records.
- Double n and predict the change in n, n log n and n².
- Trace triangular work as 1 + 2 + … + n.
One full scan costs Θ(n). An auxiliary counter costs Θ(1) space. A best case and a worst case can have different growth rates.
Watch for this mistake
Two loops in one function can be linear when each pointer advances at most n times overall.
State whether your space bound includes the returned result and the recursive call stack.