CS2: Efficiency of Algorithms
Understanding how efficient algorithms are and why some algorithms are better than others for solving the same problem.
Understanding how efficient algorithms are and why some algorithms are better than others for solving the same problem.
Two algorithms might solve the same problem, but one could be much faster or use less memory. Understanding efficiency helps you choose the best algorithm for a given situation.
Algorithm A (Linear approach): Read every name from the first page until you find the one you want.
Algorithm B (Binary approach): Open the book in the middle. If the name is earlier, discard the second half. Repeat with the remaining half.
For a phone book with 1000 names:
Algorithm B is far more efficient for large phone books!
| Input Size (n) | Linear - O(n) | Binary - O(log n) | Difference |
|---|---|---|---|
| 10 | 10 checks | 4 checks | 2.5x faster |
| 100 | 100 checks | 7 checks | 14x faster |
| 1,000 | 1,000 checks | 10 checks | 100x faster |
| 1,000,000 | 1,000,000 checks | 20 checks | 50,000x faster |
| Complexity | Name | Meaning | Example |
|---|---|---|---|
| O(1) | Constant time | Time stays the same regardless of input size | Accessing an array element by index |
| O(log n) | Logarithmic | Time increases very slowly - halves the problem each step | Binary search |
| O(n) | Linear | Time increases proportionally with input size | Linear search |
| O(n log n) | Linearithmic | Time increases slightly more than linearly | Merge sort |
| O(n²) | Quadratic | Time increases with the square of input size | Bubble sort |
A school has 10,000 student records to sort by surname.
Bubble sort (O(n²)): Up to 100,000,000 comparisons
Merge sort (O(n log n)): Approximately 140,000 comparisons
Merge sort is over 700 times faster for this dataset!
One way to measure efficiency is to count the number of key operations an algorithm performs.
FOR i ← 0 TO LENGTH(list) - 1
IF list[i] = target THEN // This comparison happens n times
OUTPUT "Found at position ", i
ENDIF
NEXT i
Comparisons: n (where n is the length of the list)
Best case: 1 comparison (item is first)
Worst case: n comparisons (item is last or not present)
Each step halves the remaining search space.
After 1 comparison: n/2 items remain
After 2 comparisons: n/4 items remain
After k comparisons: n/2^k items remain
Maximum comparisons: log₂(n)
For 1,000,000 items: only 20 comparisons needed!
| Aspect | Time Efficiency | Space Efficiency |
|---|---|---|
| Measures | How fast the algorithm runs | How much memory it uses |
| Trade-off | Using more memory can speed things up | Using less memory may slow things down |
| Example | Merge sort is fast but needs extra arrays | Bubble sort is slow but uses no extra memory |
| GCSE focus | Time complexity is the main focus | Awareness that memory matters too |
Q1: What does time complexity measure?
Q2: An algorithm has O(n) time complexity. If it takes 5 seconds to process 1000 items, approximately how long would it take to process 5000 items?
Q3: Explain why binary search is more efficient than linear search for large datasets.
Q4: Give an example of a situation where using a less efficient algorithm might be acceptable.
Q5: A bubble sort on 100 items takes up to 10,000 comparisons. How many comparisons would it take for 1000 items?
✗ Thinking a faster algorithm is always better regardless of memory usage ✓ Efficiency considers both time and space; a faster algorithm may use significantly more memory, which could be unsuitable for constrained systems.
✗ Confusing time complexity with actual running time ✓ Time complexity describes how the number of operations grows with input size (Big O), not the actual clock time a program takes to run.
✗ Believing O(n) is always worse than O(log n) for all inputs ✓ O(log n) scales better for large inputs, but for very small datasets the constant overhead of O(log n) algorithms may make O(n) faster in practice.
✗ Thinking Big O notation measures exact step counts ✓ Big O notation describes the upper bound of growth rate as input tends to infinity, ignoring constants and lower-order terms.
Two algorithms solve the same problem. Algorithm A has time complexity O(n²) and Algorithm B has O(n log n). Explain which algorithm is more efficient for large datasets and why. [4 marks]
Algorithm B (O(n log n)) is more efficient for large datasets. As n increases, n² grows much faster than n log n. For example, with n = 1000: Algorithm A performs roughly 1,000,000 operations while Algorithm B performs roughly 10,000 operations. Big O notation shows that Algorithm B scales better because its growth rate is lower, meaning it will be significantly faster for large inputs. However, for very small datasets Algorithm A might be comparable due to lower constant overhead.
AO1 (Computational Thinking — 40%): Demonstrate knowledge and understanding of the principles and concepts of computer science, including algorithm efficiency and Big O notation for AQA 8525, OCR J277 & Edexcel 1CP2.
AO2 (Application — 40%): Apply knowledge and understanding of computer science, including algorithm efficiency and Big O notation to analyse problems in computational terms and to design, write and evaluate solutions.
AO3 (Evaluation — 20%): Evaluate the effectiveness, correctness and efficiency of computational solutions, including algorithm efficiency and Big O notation, and make reasoned judgements about trade-offs.
Get the best revision books and guides to boost your grades.