Practice Flashcards
What does Big O describe?
Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.
All Flashcards in Topic 6.4
Below are all 25 flashcards for this topic. Sign up free to track your progress and get personalized review schedules.
6.4.15 cards
What does Big O describe?
How an algorithm's work grows as the data grows — the shape of the growth, not the time in seconds, which depends on the machine it runs on.
How do you work out an algorithm's complexity?
Count the loops over the data. No loop is O(1), one loop is O(n), a loop inside a loop is O(n²), and halving what is left at each step is O(log n).
Why is 2n + 5 written as O(n)?
Big O describes the shape of the growth, and constants and lower-order terms do not change that shape. Doubling the data still doubles the work.
What is the difference between time and space complexity?
Time complexity is how the number of steps grows; space complexity is how the extra memory grows, not counting the input. Bubble sort is O(n²) time but O(1) space, because it sorts in place.
What does O(n²) mean in practice?
Twice the data means four times the work, and ten times the data means a hundred times. At 1,000 items that is a million steps; at 10,000 it is a hundred million.
6.4.25 cards
How does linear search work, and what does it cost?
It checks each item in turn until it finds the target or runs out, returning the position or -1. Best case O(1) if the target is first, worst case O(n) if it is last or absent.
How does binary search work?
Look at the middle item. If it equals the target, stop. If it is too small the target is to the right, so move low to mid + 1; if too big, move high to mid - 1. Each step discards about half the remaining list.
Why must binary search have sorted data?
Discarding half depends on knowing which half the target would be in, which only holds if the list is in order. On unsorted data it does not run slowly — it returns the wrong answer.
How does binary search terminate?
When low passes high, meaning there is nothing left to search. It then returns -1 to indicate the target is absent.
How do the two searches grow with the data?
Doubling the list doubles linear search's worst case, but adds only one comparison to binary search's. Sixteen items need at most four binary comparisons; a million need about twenty.
6.4.35 cards
How does bubble sort work?
It compares each neighbouring pair and swaps any that are out of order. Each pass sends the largest remaining value to the end, so the next pass can compare one fewer pair.
How does selection sort work?
For each position it looks through the whole remaining list, remembering where the smallest value is, then swaps it into place. At most one swap per position.
How do the two sorts compare on efficiency?
Both make O(n²) comparisons. Bubble sort makes up to O(n²) swaps; selection sort makes only O(n). Bubble sort has a best case of O(n) on sorted data thanks to its early exit, while selection sort has none.
What is the space complexity of bubble and selection sort?
Both are O(1). Each sorts in place, needing only a couple of extra variables however large the list becomes.
When is selection sort the better choice?
When writing data is far more expensive than reading it. The comparison counts are identical, so the algorithm doing O(n) swaps instead of O(n²) is clearly better.
6.4.45 cards
What are the two parts of a recursive function?
A **base case** that returns without recursing, and a **recursive case** that calls itself on a smaller problem.
Why does deep recursion cause a stack overflow?
Each call keeps a **stack frame** — parameters, locals, return address — and none is released until the base case is reached.
Why do recursive calls need a stack?
They return in the **reverse order** they were made, most recent first — last in, first out.
When is recursion genuinely better than iteration?
When the **data is self-similar** — a tree, a folder structure, a nested expression. Iterating those needs an explicit stack managed by hand.
Is recursive factorial a good choice?
It is a good **teaching example**, not a good choice — a loop is faster and uses constant memory.
6.4.55 cards
What are the two halves of a recursive trace?
**Down** — each call is made and waits, with no arithmetic. **Up** — each returns a value, and the arithmetic happens.
When does the arithmetic in n + recurse(n-1) happen?
On the way **up**, once the base case has returned a value to add to. Nothing is computed descending.
How should a recursive trace be laid out?
Indent each call **one level further** than its caller; leave the return blank going down and fill it in coming back up.
What changes with two recursive calls per level?
The calls form a **tree**, the left branch completes before the right, and the same values are computed repeatedly.
Why is naive Fibonacci slow?
Calls grow **exponentially** — fib(30) makes over 1.3 million — because nothing is remembered between branches and sub-results are recomputed.
Topic 6.4 study notes
Full notes & explanations for Programming algorithms
Computer Science exam skills
Paper structures, command terms & tips
Want smart review reminders?
Sign up free to track your progress. Our spaced repetition algorithm will tell you exactly which cards to review and when.
Start Free