Pearson Edexcel 1CP2 GCSE Computer Science · Topic 1: Computational Thinking

Stretch tasks Searching and Sorting Algorithms

Task 1

Quiz Challenge

A quiz game against four computer players, then short and long written answers.

  1. Quiz Blitz12 questions against the clock
  2. Quick Fire4 short answers
  3. Big Think2 long answers

About 20 minutes

Task 2

This or That

Pixel the cat asks 8 questions. Each has two answers: pick the right one.

  • Two answers to every question
  • A fish treat for every right answer
  • Streaks win Pixel shades, then a party hat

About 4 minutes

Task 3

Memory Match

Flip the cards two at a time to match each term card to its meaning card.

  • 12 cards: each term has a meaning
  • One peek at every card
  • Combo points for pairs found in a row

6 pairs, about 4 minutes

Task 4

Sort It

Sort each statement into the right box, like the tick-box tables in the exam.

  • 12 cards to sort into 4 boxes
  • Drag a card, tap a box or press its number
  • Combo points for cards sorted in a row

12 cards, about 4 minutes

The games need JavaScript. Every question is below, with the answers at the end.

Read the lesson: Searching and Sorting Algorithms

Searching and Sorting Algorithms: the questions

Quiz Challenge, round 1: Quiz Blitz

  1. What must be true of a list before a binary search can be used?

    • It must hold numbers only
    • It must have an even length
    • It must have no duplicates
    • It must be sorted
  2. At worst, how many comparisons does a linear search make on a list of 1000 items?

  3. Searching [4, 8, 15, 16, 23, 42, 50] for 23, which item does binary search check first?

    • 23
    • 42
    • 16
    • 4
  4. After one bubble sort pass on [7, 3, 9, 2], what is the list?

    • [3, 7, 9, 2]
    • [7, 3, 2, 9]
    • [3, 7, 2, 9]
    • [2, 3, 7, 9]
  5. Bubble sort stops when a pass makes no swaps.

    • True
    • False
  6. How many comparisons does one bubble sort pass make on 8 items?

  7. What is merge sort's strategy called?

    • First come, first served
    • Divide and conquer
    • Brute force
    • Trial and error
  8. Why does merge sort split until each piece holds one item?

    • Single items use less memory
    • It cannot compare longer lists
    • It makes the list shorter
    • A list of one item is already sorted
  9. What is merge sort's main drawback?

    • It is slow on large lists
    • It only works on sorted lists
    • It cannot sort text
    • It needs extra memory for the sub lists
  10. Binary search on an unsorted list just runs more slowly.

    • True
    • False
  11. When is linear search a sensible choice?

    Tick 2.

    • The list is unsorted and searched only once
    • A million sorted items are searched all day
    • Searches run thousands of times a second
    • The list is sorted and huge
    • The list is small
  12. Merge [2, 6] with [1, 4, 8]. What is the result?

    • [1, 4, 8, 2, 6]
    • [2, 1, 6, 4, 8]
    • [1, 2, 4, 6, 8]
    • [2, 6, 1, 4, 8]

Quiz Challenge, round 2: Quick Fire

  1. Explain why binary search needs fewer comparisons than linear search on a large sorted list. [2 marks]

  2. Explain why merge sort may be a poor choice on a device with very little RAM. [2 marks]

  3. Explain the purpose of the swapped flag in bubble sort. [2 marks]

  4. Explain the difference between a pass and a comparison in bubble sort. [2 marks]

Quiz Challenge, round 3: Big Think

  1. A shop's website holds 50,000 product codes in sorted order and searches them thousands of times a day. Compare linear search and binary search for this task and justify which should be used. [6 marks]

  2. Describe how merge sort would sort the list [6, 2, 8, 4, 1], and evaluate it against bubble sort. [6 marks]

This or That: tick the right answer

  1. An unsorted list of 20 names, searched once: linear or binary search?

    • Linear search
    • Binary search
  2. Which sort suits a nearly sorted list on a device with tiny memory?

    • Merge sort
    • Bubble sort
  3. Which halves the search area with each comparison?

    • Linear search
    • Binary search
  4. One pass over 10 items makes how many comparisons?

    • 10
    • 9
  5. Which carries the largest value to the end on its first pass?

    • Bubble sort
    • Merge sort
  6. Does merge sort stop splitting at pairs of two?

    • No: down to single items
    • Yes: pairs are enough
  7. Is merge sort better than bubble sort in every way?

    • No: it needs more memory
    • Yes: it always wins
  8. The target is the first item in the list. Which search finds it in one comparison?

    • Binary search
    • Linear search

Memory Match: match each term to its meaning

TermMeaning
A Linear search1 Swaps neighbours until a pass has no swaps
B Binary search2 Solve smaller copies of a problem, then combine
C Bubble sort3 Checks each item in turn
D Merge sort4 Uses almost no extra memory
E Divide and conquer5 Checks the middle, discards half
F In place6 Splits to single items, then combines in order

Sort It: tick one box in each row

Which algorithm does each statement describe?

StatementLinear searchBinary searchBubble sortMerge sort
Works on an unsorted list
Checks each item in turn from the start
Worst case of n comparisons
Stops when a pass makes no swaps
Sorts in place, with almost no extra memory
Needs extra memory to hold the sub lists
Splits the list until each piece holds one item
Needs the list to be sorted first
Combines sorted pieces by taking the smaller front item
Discards half the remaining items each time
About 16 comparisons for 50,000 items
Swaps neighbours that are the wrong way round
Answers

Round 1: Quiz Blitz

  1. It must be sorted On an unsorted list it can give the wrong answer.
  2. 1000 The target may be last, or missing, so every item is checked.
  3. 16 It starts with the middle item.
  4. [3, 7, 2, 9] Swap 7 and 3, leave 7 and 9, swap 9 and 2. The 9 has bubbled to the end.
  5. True The swapped flag tells it the list is in order.
  6. 7 n items give n minus 1 comparisons.
  7. Divide and conquer Split into halves, then combine the sorted pieces.
  8. A list of one item is already sorted Merging two sorted lists is then easy.
  9. It needs extra memory for the sub lists Bubble sort works in place with almost no extra memory.
  10. False It can discard the half that holds the target, and return the wrong answer.
  11. The list is small; The list is unsorted and searched only once It needs no sorting, and on a small list it is quick enough.
  12. [1, 2, 4, 6, 8] Compare the front items and take the smaller each time.

Round 2: Quick Fire

    • Each comparison discards half the remaining items
    • Linear search rules out only one item per comparison
    Model answer

    Each binary search comparison discards half of the items that are left, so a million items need about 20 comparisons. Linear search checks each item in turn, ruling out only one item per comparison.

    • It holds the sub lists separately, needing extra memory
    • Bubble sort works in place, needing almost no extra memory
    Model answer

    Merge sort has to hold the sub lists separately while it merges them, so it needs extra memory roughly the size of the original list. Bubble sort sorts in place, needing room for only one temporary variable.

    • It records whether any swap happened during a pass
    • If a pass makes no swaps, the algorithm stops early
    Model answer

    The flag records whether any swap was made during a pass. If a whole pass makes no swaps, the list must be in order, so the algorithm stops instead of making pointless extra passes.

    • A pass is one complete run through the list
    • A comparison tests two neighbouring values, and a pass makes n - 1 of them
    Model answer

    A pass is one complete run through the list. A comparison is a single test of two adjacent values, and one pass over n items makes n minus 1 comparisons.

Round 3: Big Think

    • Linear search checks each code in turn
    • So it may need 50,000 comparisons
    • Linear search works on a list in any order
    • Binary search checks the middle and discards half each time
    • So it needs only about 16 comparisons
    • The codes are already sorted, so its precondition costs nothing
    • Over thousands of searches the saving is enormous
    • A justified choice of binary search
    Model answer

    Linear search checks each code in turn from the start, so in the worst case it makes 50,000 comparisons, although it works on a list in any order. Binary search checks the middle code and discards half of the remaining codes with each comparison, so it needs at most about 16 comparisons. Its precondition, a sorted list, costs nothing here, because the codes are already sorted. Over thousands of searches a day the saving is enormous, giving faster responses and less work for the server. I would choose binary search.

    • The list is split in half again and again
    • Until each sub list holds a single item
    • Sorted pieces are combined by taking the smaller front item
    • Merge sort is much faster on large lists
    • Bubble sort makes many slow passes
    • Merge sort needs extra memory, while bubble sort works in place
    • Bubble sort is easier to write, and good on nearly sorted data
    • A conclusion weighing the two
    Model answer

    Merge sort splits the list in half again and again: [6, 2] and [8, 4, 1], then [6], [2], [8] and [4, 1], until every sub list holds a single item, which is already sorted. It then combines pairs of sorted lists by comparing their first items and taking the smaller each time: [2, 6], then [1, 4], then [1, 4, 8], and finally [1, 2, 4, 6, 8]. Compared with bubble sort, merge sort is much faster on large lists, because it needs only about log n rounds, whereas bubble sort makes many slow passes. However, merge sort needs extra memory for the sub lists, while bubble sort works in place and is easier to write. For five items either is fine, so on balance merge sort only wins clearly when the list is large.

This or That

  1. Linear search Binary search needs a sorted list, and sorting first would cost more than the search saves.
  2. Bubble sort Bubble sort finishes in a pass or two, and sorts in place with almost no extra memory.
  3. Binary search Linear search rules out just one item per comparison.
  4. 9 A pass compares neighbours, so n items give n minus 1 comparisons.
  5. Bubble sort That bubbling up is where the name comes from.
  6. No: down to single items Division continues until every sub list holds exactly one item.
  7. No: it needs more memory Merge sort holds the sub lists separately, so it needs extra memory.
  8. Linear search Linear search starts at the front. Binary search starts in the middle.

Memory Match

  • A 3 Linear search: Checks each item in turn
  • B 5 Binary search: Checks the middle, discards half
  • C 1 Bubble sort: Swaps neighbours until a pass has no swaps
  • D 6 Merge sort: Splits to single items, then combines in order
  • E 2 Divide and conquer: Solve smaller copies of a problem, then combine
  • F 4 In place: Uses almost no extra memory

Sort It

  1. Linear search Works on an unsorted list Linear search makes no demands on the data.
  2. Linear search Checks each item in turn from the start That is linear search.
  3. Linear search Worst case of n comparisons Linear search may have to check every item.
  4. Bubble sort Stops when a pass makes no swaps Bubble sort's swapped flag.
  5. Bubble sort Sorts in place, with almost no extra memory Bubble sort needs just one temporary variable.
  6. Merge sort Needs extra memory to hold the sub lists Merge sort's main cost.
  7. Merge sort Splits the list until each piece holds one item That is merge sort's divide step.
  8. Binary search Needs the list to be sorted first Binary search's precondition.
  9. Merge sort Combines sorted pieces by taking the smaller front item That is merge sort's merge step.
  10. Binary search Discards half the remaining items each time That is binary search.
  11. Binary search About 16 comparisons for 50,000 items Halving 50,000 takes about 16 steps.
  12. Bubble sort Swaps neighbours that are the wrong way round That is bubble sort.