Algorithms and Problem Solving · Lesson 3.4.1–3.4.2
Algorithm Analysis: Time and Space Complexity
Think in steps, not seconds: best and worst cases, halving growth, and choosing the data structure that does the heavy lifting.
01
Learning objectives
Count the steps a loop performs and describe how the count grows with input size n.
Distinguish the best, average and worst case of a search.
Explain why doubling the data adds only about one step to a halving algorithm.
Choose between a list and a set for membership testing, with justification.
Estimate an answer with rough numbers before writing any code.
02
Lesson notes
Two algorithms can both be correct while one takes a million steps and the other twenty. Algorithmic thinking means predicting that difference before running anything: count the steps — comparisons, assignments, loop passes — as a function of the input size n. A loop over n items does about n steps; a loop inside a loop does about n × n; repeated halving does only about log₂(n) steps.
Searches have three famous answers. Best case: the target sits first, and linear search finishes in 1 comparison. Worst case: the target is absent or last — all n comparisons. Average case: about n/2. Binary search's worst case on n sorted items is about log₂(n) + 1: a million items need roughly 20 comparisons, and doubling the data adds only one more. That is why sorted structure beats raw speed.
You can feel the halving: from 100, repeated halving to 1 takes 6 steps (100, 50, 25, 12, 6, 3, 1); from 1,000 it takes 9. Grow the data a thousandfold and the step count grows by three — this flat growth is what makes big systems possible. Contrast a nested loop over the same data: 1,000 items mean 1,000,000 inner steps, and doubling the data quadruples the work.
Choosing the right data structure is half of efficiency. Checking whether roll number 4471 is enrolled against a 50,000-element list scans up to 50,000 values every time; against a set, the same question is answered in a handful of hash steps, however big the register. Decide by the dominant operation: frequent membership tests want a set, ordered processing and position access want a list, key-based lookup wants a dictionary.
Estimation closes the loop: before coding, rough the numbers out — 50,000 records at 10 checks per second is 500,000 comparisons a second, which will crawl, so use a set. The skill examiners probe is reasoning about growth, not naming formulas: given two loops, say which scales worse and why, with a concrete count for a stated n. Practise producing the counts, not just the adjectives.
03
Worked code examples
Counting the real comparisons
The worst-case target 100 costs linear search 100 comparisons but binary search only 7 — the halving window reaches it in log-style steps.
def linear_steps(nums, target):
steps = 0
for i in range(len(nums)):
steps = steps + 1
if nums[i] == target:
return steps
return steps
def binary_steps(nums, target):
low, high = 0, len(nums) - 1
steps = 0
while low <= high:
steps = steps + 1
mid = (low + high) // 2
if nums[mid] == target:
return steps
elif nums[mid] < target:
low = mid + 1
else:
high = mid - 1
return steps
data = list(range(1, 101))
print(linear_steps(data, 100))
print(binary_steps(data, 100))
Output
100
7
Halving growth you can count
100 needs 6 halvings to reach 1, and 1000 needs 9. Multiplying the data by 10 adds only about 3 steps — that is logarithmic growth.
def halving_steps(n):
total = 0
while n > 1:
total = total + 1
n = n // 2
return total
print(halving_steps(100))
print(halving_steps(1000))
Output
6
9
Best and worst for the same search
The first position is the best case (1 comparison); the last element and a total miss are the worst case (5 comparisons on this 5-item list).
def linear_search(nums, target):
for i in range(len(nums)):
if nums[i] == target:
return i + 1
return len(nums)
data = [7, 3, 9, 1, 8]
print(linear_search(data, 7))
print(linear_search(data, 8))
print(linear_search(data, 100))
Output
1
5
5
04
Key terminology
step count
The number of basic operations an algorithm performs, counted against input size.
best case
The luckiest input — for linear search, the target at position 0.
worst case
The unluckiest input — target absent or last — used for performance guarantees.
average case
The expected cost over all inputs — about n/2 comparisons for linear search.
logarithmic growth
Growth like log₂(n): doubling the data adds only about one step.
05
Common misconceptions
Common mistake
A faster computer will make a slow algorithm acceptable.
Correct understanding
Hardware gives a constant speed-up, while algorithmic growth multiplies with the data. A linear scan still drowns as n grows, whatever the CPU.
Common mistake
The best case is what matters when promising performance.
Correct understanding
Users hit the worst case — missing values, last elements. Guarantees are stated in worst-case or average-case terms.
Common mistake
Fewer lines of code means a faster program.
Correct understanding
Step count, not line count, decides speed; a compact nested loop can do a million times more work than a longer single loop.
06
Check your understanding
1. Worst-case comparisons for linear search on 200 items?
2. About how many comparisons does binary search need on 1,000,000 sorted items?
3. An app asks 'is this roll number enrolled?' millions of times on a 50,000-student register. Best structure?