Back to the topic

The mark scheme starts on a new page, so you can print pages one and two for a student and keep the last page yourself. Everything prints in black on white whichever theme you are using.

Python · GCSE 6

Searching and Sorting Algorithms

Section A: 10 multiple choice questions, 10 marks. Section B: 5 written questions, 18 marks. Total 28 marks.

Name

Class

Date

Mark  / 28

Section A

Circle the letter of the correct answer. One mark each.

  1. 1What must be true of a list before a binary search can be used?1
    1. AIt must have no duplicates
    2. BIt must have an even number of items
    3. CIt must be sorted
    4. DIt must hold only numbers
  2. 2A binary search of [2, 4, 6, 8, 10, 12, 14] begins by comparing with which item?1
    1. A2
    2. B6
    3. C8
    4. D14
  3. 3Which search is the better choice for one search of a short list that is not sorted?1
    1. ANeither can be used
    2. BA linear search
    3. CA binary search
    4. DEither, because they are equally fast
  4. 4What does a bubble sort do on each pass?1
    1. APicks the smallest item and moves it to the front
    2. BSplits the list in half
    3. CSearches for the middle item
    4. DCompares neighbouring items and swaps them if they are in the wrong order
  5. 5What is the list [5, 1, 4, 2, 8] after the first pass of a bubble sort?1
    1. A[1, 2, 4, 5, 8]
    2. B[5, 1, 4, 2, 8]
    3. C[1, 4, 2, 5, 8]
    4. D[1, 5, 4, 2, 8]
  6. 6How does a bubble sort know it has finished?1
    1. AIt has made one pass
    2. BIt has reached the middle of the list
    3. CIt has found the target
    4. DA whole pass makes no swaps
  7. 7Which technique does a merge sort use?1
    1. ADivide and conquer
    2. BSearching for the middle
    3. CInserting one item at a time
    4. DSwapping neighbours
  8. 8What does an insertion sort do with each item in turn?1
    1. AInserts it into the correct place among the items already sorted
    2. BCompares it with its neighbour and swaps them if they are in the wrong order
    3. CSplits the list in half and sorts each half separately
    4. DLooks for the smallest item left and swaps it into the next place
  9. 9Why is a merge sort quicker than a bubble sort on a very large list?1
    1. AIt uses no memory
    2. BIt does not compare items at all
    3. CIt only works on small lists
    4. DIt needs far fewer comparisons
  10. 10Which of these sorts needs extra memory for copies of parts of the list?1
    1. AInsertion sort
    2. BMerge sort
    3. CAll three need the same
    4. DBubble sort

Section B

Answer in the space provided. The number of marks tells you how many separate points to make.

  1. 1DescribeThe list shown is sorted. Describe the steps a binary search takes to find the value 28.4
    Position0123456
    Value391417222835
  2. 2WriteWrite a Python function called linear_search that takes a list and a target and returns the position of the target in the list, or -1 if it is not in the list.4
  3. 3ShowShow the contents of the list after each of the first two passes of a bubble sort on the list [6, 2, 9, 4]. State whether a third pass is needed.3
  4. 4ExplainA programmer must find one name in an unsorted list of 20 names, once. Explain why a linear search is a better choice than sorting the list and then using a binary search.3
  5. 5CompareCompare a merge sort with a bubble sort for sorting a list of one million names.4

Mark scheme

Section A

1C2C3B4D5C6D7A8A9D10B

Section B

1 The list shown is sorted. Describe the steps a binary search takes to find the value 28. 4

One mark for each of the following, to a maximum of 4.

  • Finds the middle item, which is 17
  • Compares 28 with 17 and discards the lower half because 28 is larger
  • Takes the middle of the remaining items, which is 28
  • Compares and finds 28, so the search is complete

Model answer. The middle item of the seven is 17, at position 3. 28 is larger than 17, so the lower half and 17 itself are discarded, leaving 22, 28 and 35. The middle of those is 28, which matches, so the search stops and reports that 28 is at position 5.

2 Write a Python function called linear_search that takes a list and a target and returns the position of the target in the list, or -1 if it is not in the list. 4

One mark for each of the following, to a maximum of 4.

  • Defines a function with two parameters, the list and the target
  • A loop that goes through the positions of the list
  • Compares each item with the target and returns the position when equal
  • Returns -1 after the loop, once every item has been checked

Model answer.

Python
def linear_search(items, target):
    for i in range(len(items)):
        if items[i] == target:
            return i
    return -1

print(linear_search([4, 8, 15, 16], 15), linear_search([4, 8, 15, 16], 99))

Each position is checked in turn and returned as soon as the item matches. The return of -1 is outside the loop, so it only happens when every item has been checked and none matched. The calls shown print 2 and -1.

3 Show the contents of the list after each of the first two passes of a bubble sort on the list [6, 2, 9, 4]. State whether a third pass is needed. 3

One mark for each of the following, to a maximum of 3.

  • After the first pass the list is [2, 6, 4, 9]
  • After the second pass the list is [2, 4, 6, 9]
  • A third pass is needed to confirm that no swaps are made

Model answer. First pass: 6 and 2 swap to give 2, 6, 9, 4. 6 and 9 are in order. 9 and 4 swap, so the list is [2, 6, 4, 9]. Second pass: 2 and 6 are in order, 6 and 4 swap, giving [2, 4, 6, 9]. Another pass is needed, because the second pass made a swap, and it will confirm there are no swaps so the algorithm can stop.

4 A programmer must find one name in an unsorted list of 20 names, once. Explain why a linear search is a better choice than sorting the list and then using a binary search. 3

One mark for each of the following, to a maximum of 3.

  • A binary search can only be used on a sorted list
  • Sorting the list takes more steps than one linear search would
  • The search is only done once so there is no benefit from sorting

Model answer. A binary search only works on a sorted list, so the names would have to be sorted first. Sorting 20 names takes more comparisons than simply checking all 20 once with a linear search. Because the search is only carried out once, there are no later searches to benefit from the sorted list, so the sorting would be wasted effort.

5 Compare a merge sort with a bubble sort for sorting a list of one million names. 4

One mark for each of the following, to a maximum of 4.

  • Merge sort needs far fewer comparisons on a large list
  • Because it halves the list repeatedly, whereas a bubble sort compares neighbours over many passes
  • Merge sort needs extra memory to hold the merged lists
  • Bubble sort sorts in place and is simpler to write, but is too slow for a million items

Model answer. A merge sort is much better for a million names because it needs far fewer comparisons. It splits the list in half repeatedly and merges sorted halves, so the work grows slowly as the list grows, where a bubble sort makes many passes comparing neighbours and its work quadruples when the list is doubled. The cost of merge sort is that it needs extra memory for the lists it builds, whereas a bubble sort works in place and is simpler to write, but it would be far too slow for a million items.