Subject: Computer Science | Level: A-Level | Exam Board: OCR
Master OCR A-Level Computer Science Algorithms (2.1) with this comprehensive guide. We'll break down algorithm analysis using Big O notation, explore standard sorting and searching algorithms, and demystify pathfinding with Dijkstra's and A*. This guide is packed with exam-focused advice, worked examples, and memory hooks to help you secure top marks.
Revision Notes & Key Concepts
Worked Examples
Worked Example
Question: The following pseudocode algorithm performs a sort on the array `data`. State the name of the sorting algorithm shown and trace its execution with the array `[6, 2, 8, 4]`. pseudocode procedure mySort(data) for i = 1 to len(data) - 1 key = data[i] j = i - 1 while j >= 0 and key < data[j] data[j+1] = data[j] j = j - 1 endwhile data[j+1] = key next i endprocedure
Solution: **Algorithm Name**: Insertion Sort **Trace Table**: | i | key | j | data[j] | Condition (key < data[j]) | data Array State | |---|---|---|---|---|---| | - | - | - | - | - | `[6, 2, 8, 4]` | | 1 | 2 | 0 | 6 | `2 < 6` is True | `[6, 6, 8, 4]` | | | | -1| - | Loop ends | `[2, 6, 8, 4]` | | 2 | 8 | 1 | 6 | `8 < 6` is False | `[2, 6, 8, 4]` | | 3 | 4 | 2 | 8 | `4 < 8` is True | `[2, 6, 8, 8]` | | | | 1 | 6 | `4 < 6` is True | `[2, 6, 6, 8]` | | | | 0 | 2 | `4 < 2` is False | `[2, 4, 6, 8]` | **Final Answer**: The array is `[2, 4, 6, 8]`.
Worked Example
Question: Compare the time and space complexity of Merge Sort and Quick Sort. Explain the trade-offs a developer might consider when choosing between them.
Solution: **Comparison**: * **Time Complexity**: Both Merge Sort and Quick Sort have an average and best-case time complexity of **O(n log n)**. However, Quick Sort has a worst-case time complexity of **O(n^2)**, which occurs with poor pivot selection, whereas Merge Sort's worst-case is always **O(n log n)**. * **Space Complexity**: Merge Sort requires **O(n)** auxiliary space because it needs temporary arrays to merge the sorted halves. Quick Sort is an in-place sort and has a space complexity of **O(log n)** to store the recursion stack. **Trade-offs**: A developer might choose **Quick Sort** when average-case performance is a priority and memory is limited, as it's generally faster in practice and uses less space. They might choose **Merge Sort** when guaranteed performance is critical and the worst-case scenario must be avoided (e.g., in real-time systems), and when the extra memory usage is acceptable.
Worked Example
Question: A programmer is implementing a binary search. Explain why the data must be sorted beforehand and describe the steps the algorithm would take to find the number 23 in the array `[4, 7, 11, 15, 23, 28, 31]`.
Solution: **Reason for Sorting**: A binary search works by repeatedly dividing the search interval in half. It relies on the assumption that if the target value is not the middle element, it must lie exclusively in either the lower or upper half of the list. This is only possible if the list is ordered. (1 mark) **Search Steps**: 1. **Initial State**: `low = 0`, `high = 6`, `mid = 3`. The element at `mid` is `15`. 2. **Step 1**: `23 > 15`, so the target must be in the upper half. The new search interval becomes `low = 4`, `high = 6`. 3. **Step 2**: `mid = (4+6)/2 = 5`. The element at `mid` is `28`. 4. **Step 3**: `23 < 28`, so the target must be in the lower half. The new search interval becomes `low = 4`, `high = 4`. 5. **Step 4**: `mid = (4+4)/2 = 4`. The element at `mid` is `23`. The target is found. (3 marks for correct steps)
Practice Questions
Question: State the worst-case time complexity of Quick Sort and describe a scenario that would cause it to occur.
Answer:
Question: A developer has a list of 1 million unsorted items. They need to check for the presence of a specific item. Evaluate the suitability of using a binary search for this task.
Answer:
Question: Describe the steps a Merge Sort algorithm would take to sort the array `[5, 1, 9, 3]`. You may use diagrams to support your answer.
Answer:
Question: Explain the difference between time complexity and space complexity.
Answer:
Question: What is meant by a 'stable' sorting algorithm? Give an example of a stable and an unstable sort.
Answer: