Chapters List
- A Mental Model for Data Structures and Algorithms
- Why Data Structures Exist
- Measuring Efficiency: Time and Space
- Arrays: The Foundation of Data Storage
- Linked Structures
- Stacks and Queues: Controlling Order
- Hash Tables: Fast Lookup
- Trees: Hierarchical Data
- Graphs: Modeling Relationships
- Common Algorithm Techniques
- Sorting and Searching Algorithms
- Data Structures in Modern Systems
data structures and algorithms
Measuring Efficiency: Time and Space
Learn how to reason about runtime and memory growth using Big-O intuition.
In the previous chapter we saw that structure choices affect speed.
Now we need language to measure that effect.
Two Core Metrics
When evaluating an algorithm, we usually track:
- Time: how runtime grows as input size grows
- Space: how extra memory usage grows
The key word is growth, not exact milliseconds.
Why Growth Matters
Exact runtime depends on hardware and implementation details.
But growth behavior is stable and useful for comparison.
If input size doubles, does work double? Triple? Stay similar?
That is the heart of complexity analysis.
Big-O Intuition
Big-O describes how work scales with input size n.
Common growth patterns:
O(1)constantO(log n)slowly growingO(n)linearO(n log n)efficient for many sorting tasksO(n^2)often too slow at large scale
No heavy math required here. Think in terms of trend.
Linear Search vs Binary Search
Linear Search
Check items one by one.
Worst-case behavior is O(n).
Binary Search
On sorted data, repeatedly cut search space in half.
Worst-case behavior is O(log n).
graph LR
A[Start with n items] --> B[Linear: check one by one]
A --> C[Binary: halve each step]
For very large n, this difference is dramatic.
Time vs Space Tradeoff
Sometimes we use more memory to reduce runtime.
Example: caching previous results can speed repeated computations.
So the best choice depends on system constraints:
- memory budget
- latency requirements
- workload shape
Key Ideas to Remember
- Efficiency is about growth as input size increases.
- Time complexity and space complexity are both important.
- Big-O is a model for comparing growth trends.
O(log n)often scales better thanO(n)for search.- Faster runtime may require extra memory.
What Comes Next
With efficiency intuition in place, we can start with the most fundamental structure:
arrays and indexed storage.