Question bank › A-level Computer Science › Paper 1 – Programming, data structures & algorithms

Algorithms (graph traversal, tree traversal, Dijkstra, searching & sorting, RPN)

Answer on paper first, then reveal the mark scheme one mark at a time.

1Core3 marks

The list below is sorted in ascending order, with indexes starting at 0.

[3, 8, 12, 17, 23, 29, 35, 41, 50]

A binary search uses mid = (low + high) DIV 2, starting with low = 0 and high = 8.

(a) Show the values of mid and the item examined at each step when searching for 12. [2]

(b) State why a binary search cannot be used on an unsorted list. [1]

2Standard5 marks

The binary tree below stores integers.

(a) Write the order in which the nodes are visited in a pre-order traversal. [1]

(b) Write the order in which the nodes are visited in an in-order traversal. [1]

(c) Write the order in which the nodes are visited in a post-order traversal. [1]

(d) The tree is a binary search tree. Explain how your answer to part (b) shows this. [2]

40256015325075
3Hard6 marks

The weighted graph below shows the distances, in km, between six locations.

Dijkstra's algorithm is used to find the shortest route from A to every other node. At each step, the unfinalised node with the smallest working distance is permanently labelled (finalised), and the working distance of each of its unfinalised neighbours is updated if the route through the finalised node is shorter.

(a) State the order in which the nodes are permanently labelled (finalised). [2]

(b) State the length of the shortest path from A to F. [1]

(c) State the shortest path from A to F. [1]

(d) Explain why the working value for B changes from 4 to 3 during the algorithm. [1]

(e) State one real-world application of Dijkstra's shortest path algorithm. [1]

4215810263ABCDEF
4Standard4 marks

(a) Convert the infix expression (3 + 4) * (8 - 2) to reverse Polish notation (RPN). [2]

(b) Evaluate the RPN expression 5 1 2 + 4 * + 3 -. Show the contents of the stack as you work. [2]

5Standard5 marks

A bubble sort is used to sort [6, 2, 9, 4, 1] into ascending order. Each pass compares adjacent pairs from the start of the list, swapping them if they are out of order. The algorithm stops after a pass in which no swaps are made. No optimisation is made to shorten later passes.

(a) Write the contents of the list at the end of each pass until the algorithm stops. [3]

(b) State the total number of comparisons made. [1]

(c) State the worst-case time complexity of bubble sort. [1]

6Hard6 marks

The undirected graph below is stored as an adjacency list. When choosing which neighbour to visit next, always choose the neighbour that is earliest alphabetically.

(a) Write the order in which nodes are visited in a breadth-first traversal starting at A. [2]

(b) Write the order in which nodes are visited in a depth-first traversal starting at A. [2]

(c) State the data structure used to hold nodes waiting to be visited in a breadth-first traversal. [1]

(d) State the data structure that a depth-first traversal uses, either explicitly or through recursion. [1]

ABCDEFG
7Hard5 marks

A merge sort is used to sort [38, 27, 43, 3, 9, 82, 10] into ascending order. When a list has an odd number of items, the extra item goes in the left half.

(a) Show the two lists produced by the first split. [1]

(b) The lists are split until each contains one item, and are then merged back in pairs. Show the lists that exist after the first round of merging (a list with no partner is left unchanged). [2]

(c) Show the final sorted list. [1]

(d) State the time complexity of merge sort. [1]

8Stretch7 marks

A sorted list Data is stored in an array with indexes starting at 0.

(a) Write a recursive subroutine BinarySearch(Data, target, low, high) that returns the index of target within Data[low..high], or −1 if it is not present. The middle index must be found using (low + high) DIV 2. You may write your answer in pseudo-code or a programming language you have studied. [4]

(b) The list contains 16 items. State the greatest number of items that need to be examined by the search in the worst case. [1]

(c) Explain why a recursive binary search uses more memory than an iterative binary search of the same list. [2]

Still losing marks on this topic?

Book a free introductory session and we'll find exactly which step is costing you.

Book a free intro