Skip to content

2.1.3 Searching and sorting algorithms Practice Questions

GCSE · Paper 2 · Computational thinking, algorithms and programming · 12 practice questions · 47 marks

Exam questions for 2.1.3 Searching and sorting algorithms

12 practice questions (47 marks) with mark schemes

Answer each question, reveal the mark scheme, then award yourself marks. Scores and answers are saved in this browser so you can see which topics need more work.

Practice questions

  1. Question 1Practice question · J277/02 style2 marks

    (a) State the condition that must be true about a list before a binary search can be used. [1] (b) Explain why this condition is needed. [1]

    Show mark scheme

    (a) The list must be sorted / in order (1) (b) The algorithm compares the target with the middle item to decide which half to throw away. This only works if the items are in order (1)

    Mark yourself: how many marks did you get?
    / 2
  2. Question 2Practice question · J277/02 style4 marks

    A linear search is used to look for a value in this list of numbers.

    Index012345
    Value147229315

    (a) The target is 9. State the values that are compared with the target, in order. [2] (b) State the index at which 9 is found. [1] (c) The target is 40. State how many comparisons are made. [1]

    Show mark scheme

    (a) 14, 7, 22, 9 (2; award 1 if the order is right but there is one error, or the list stops at 9 but one value is missing) (b) 3 (1) (c) 6 (1), because every item is checked and 40 is not in the list

    Mark yourself: how many marks did you get?
    / 4
  3. Question 3Practice question · J277/02 style5 marks

    A binary search is used to look for the target 41 in this sorted list.

    Index012345678
    Value4913182227354150

    mid is worked out using (low + high) DIV 2. (a) Complete the table. [3]

    lowhighmidValue at mid
    08

    (b) State the index at which 41 is found. [1] (c) A linear search is used to find 41 in the same list. State how many comparisons it needs. [1]

    Show mark scheme

    (a)

    lowhighmidValue at mid
    08422
    58635
    78741

    Rows 2 and 3 correct (1 each); low and high changed correctly after each comparison (1). Method: 22 is smaller than 41, so low = 4 + 1 = 5. mid = (5 + 8) DIV 2 = 6. 35 is smaller than 41, so low = 7. mid = (7 + 8) DIV 2 = 7. (b) 7 (1) (c) 8 (1), because 41 is the 8th item and each item up to it is compared in turn

    Mark yourself: how many marks did you get?
    / 5
  4. Question 4Practice question · J277/02 style5 marks

    This function is called with the array nums, which holds 3, 8, 15, 20, 26, 31 (indexes 0 to 5), a size of 6 and a target of 17.

    01 function mystery(items, size, target)
    02     low = 0
    03     high = size - 1
    04     while low <= high
    05         mid = (low + high) DIV 2
    06         if items[mid] == target then
    07             return mid
    08         elseif items[mid] < target then
    09             low = mid + 1
    10         else
    11             high = mid - 1
    12         endif
    13     endwhile
    14     return -1
    15 endfunction
    16 print(mystery(nums, 6, 17))

    (a) Name the algorithm. [1] (b) Complete a trace table for low, high and mid. [3]

    lowhighmid
    05

    (c) State the value that is printed. [1]

    Show mark scheme

    (a) Binary search (1) (b)

    lowhighmid
    052
    354
    333
    32

    Rows with mid 2, 4 and 3 correct (2); low and high correct at each row, ending with low 3 and high 2 (1). Method: items[2] is 15, which is smaller than 17, so low = 3. mid = (3 + 5) DIV 2 = 4. items[4] is 26, which is bigger, so high = 3. mid = (3 + 3) DIV 2 = 3. items[3] is 20, which is bigger, so high = 2. Now low is bigger than high and the loop ends. (c) -1 (1), because 17 is not in the array

    Mark yourself: how many marks did you get?
    / 5
  5. Question 5Practice question · J277/02 style4 marks

    A bubble sort is used to sort this list from smallest to largest. 7, 4, 9, 2, 6 (a) Write down the list at the end of the first pass. [2] (b) State how many swaps are made in the first pass. [1] (c) Write down the list at the end of the second pass. [1]

    Show mark scheme

    (a) 4, 7, 2, 6, 9 (2; award 1 if only one item is in the wrong place). Method: 7 and 4 swap, 7 and 9 stay, 9 and 2 swap, 9 and 6 swap. (b) 3 (1) (c) 4, 2, 6, 7, 9 (1). Method: 4 and 7 stay, 7 and 2 swap, 7 and 6 swap.

    Mark yourself: how many marks did you get?
    / 4
  6. Question 6Practice question · J277/02 style4 marks

    An insertion sort is used to sort this list from smallest to largest. 5, 2, 9, 1, 7 Write down the whole list after each of the four items 2, 9, 1 and 7 has been inserted. [4]

    Show mark scheme

    After inserting 2: 2, 5, 9, 1, 7 (1) After inserting 9: 2, 5, 9, 1, 7 (1) After inserting 1: 1, 2, 5, 9, 7 (1) After inserting 7: 1, 2, 5, 7, 9 (1) Accept a table. The unsorted items that have not been inserted yet must still be shown.

    Mark yourself: how many marks did you get?
    / 4
  7. Question 7Practice question · J277/02 style4 marks

    Two sorted lists are merged into one sorted list as part of a merge sort. List A: 3, 8, 12 List B: 5, 6, 20 (a) Write down the merged list. [2] (b) State the first two values that are compared and which one is written to the new list first. [1] (c) State how many comparisons are made before only one list has items left. [1]

    Show mark scheme

    (a) 3, 5, 6, 8, 12, 20 (2; award 1 if only one item is out of place) (b) 3 and 5 are compared and 3 is written first (1) (c) 5 (1). Method: 3 and 5, 8 and 5, 8 and 6, 8 and 20, 12 and 20. After 12 is written, list A is empty and 20 is added with no more comparisons.

    Mark yourself: how many marks did you get?
    / 4
  8. Question 8Practice question · J277/02 style4 marks

    Describe how a merge sort sorts a list of items. [4]

    Show mark scheme

    The list is split into two halves (1) Each half is split again and again until every sublist has just one item (1) Pairs of sublists are merged, by comparing their first items and taking the smaller each time, so the new sublist is sorted (1) Merging is repeated until there is one sorted list (1)

    Mark yourself: how many marks did you get?
    / 4
  9. Question 9Practice question · J277/02 style3 marks
    01 array items = [5, 3, 8, 1]
    02 for pass = 1 to 3
    03     for i = 0 to 3 - pass
    04         if items[i] > items[i + 1] then
    05             temp = items[i]
    06             items[i] = items[i + 1]
    07             items[i + 1] = temp
    08         endif
    09     next i
    10 next pass

    (a) Name the sorting algorithm that is used. [1] (b) State the contents of the array after the program has run. [1] (c) Describe what lines 05 to 07 do. [1]

    Show mark scheme

    (a) Bubble sort (1) (b) 1, 3, 5, 8 (1). Method: pass 1 gives 3, 5, 1, 8. Pass 2 gives 3, 1, 5, 8. Pass 3 gives 1, 3, 5, 8. (c) They swap the values of items[i] and items[i + 1], using a temporary variable to hold one value (1)

    Mark yourself: how many marks did you get?
    / 3
  10. Question 10Practice question · J277/02 style4 marks

    A website has a list of 20 000 usernames. The list is not sorted. The website needs to find out whether one username is in the list. (a) Explain why a binary search cannot be used on this list as it is. [1] (b) Give one reason why a programmer may choose a linear search for this job. [1] (c) The usernames will now be searched many thousands of times a day. Explain why it may be better to sort the list first and use a binary search. [2]

    Show mark scheme

    (a) A binary search needs the data to be sorted and this list is not (1) (b) It works on unsorted data, so no sorting is needed / it is simple to write / sorting the list for a single search would take longer than searching it (1) (c) Once the list is sorted, each search needs far fewer comparisons than a linear search (1), so the time spent sorting is made up for by the many searches / searches are quicker (1)

    Mark yourself: how many marks did you get?
    / 4
  11. Question 11Practice question · J277/02 style4 marks

    A teacher says that bubble sort is easy to understand but is a poor choice for a long list. (a) Explain why a bubble sort is slow for a long list. [2] (b) State one reason why a merge sort is usually faster on a long list. [1] (c) State one disadvantage of a merge sort compared with a bubble sort. [1]

    Show mark scheme

    (a) It makes many passes through the list (1) and compares many pairs of items, swapping them one place at a time, so a long list needs a very large number of comparisons (1) (b) It splits the list into smaller lists and merges them, so it needs far fewer comparisons (1) (c) It is harder to write / it needs extra memory to hold the sublists (1)

    Mark yourself: how many marks did you get?
    / 4
  12. Question 12Practice question · J277/02 style4 marks
    01 array items = [6, 3, 8, 2]
    02 for i = 1 to 3
    03     current = items[i]
    04     j = i - 1
    05     while j >= 0 AND items[j] > current
    06         items[j + 1] = items[j]
    07         j = j - 1
    08     endwhile
    09     items[j + 1] = current
    10 next i

    (a) Name the sorting algorithm that is used. [1] (b) State the contents of the array at the end of the pass in which i is 1. [1] (c) State the contents of the array after the program has run. [1] (d) State how many times line 06 runs altogether. [1]

    Show mark scheme

    (a) Insertion sort (1) (b) 3, 6, 8, 2 (1). Method: current is 3. 6 is bigger, so 6 shifts right and 3 goes into index 0. (c) 2, 3, 6, 8 (1) (d) 4 (1). Method: 1 shift when i is 1 (the 6), 0 shifts when i is 2 because 6 is not bigger than 8, and 3 shifts when i is 3 (the 8, then the 6, then the 3).

    Mark yourself: how many marks did you get?
    / 4
General Exam Advice
  • •Practice answering questions under timed conditions
  • •Check your answers against the mark scheme carefully
  • •Focus on understanding the marking points, not just memorising answers
  • •Come back to the questions you dropped marks on a few days later