OCR J277 GCSE Computer Science · 2.1 Algorithms
Stretch tasks Searching and Sorting Algorithms
Quiz Challenge
A quiz game against four computer players, then short and long written answers.
- Quiz Blitz12 questions against the clock
- Quick Fire4 short answers
- Big Think2 long answers
About 20 minutes
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
Memory Match
Flip the cards two at a time to match each algorithm card to its how it works card.
- 12 cards: each algorithm has a how it works
- One peek at every card
- Combo points for pairs found in a row
6 pairs, about 4 minutes
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
What must be true of a list before a binary search can be used?
- It must have an even number of items
- It must contain numbers only
- It must have fewer than 100 items
- It must be sorted
How does bubble sort work?
- Splits the list in half again and again
- Inserts each item into a sorted section
- Picks a random item and moves it
- Compares adjacent items and swaps them if they are in the wrong order
Merge sort uses more memory than bubble sort.
- True
- False
A linear search checks a list of 50 items. In the worst case, how many comparisons does it make?
Put the steps of binary search in order.
Number these 1 to 4.
- If it is the target, stop
- Discard the half that cannot contain the target
- Repeat on the remaining half
- Find the middle item
Which sort is best for data that is almost in order already?
- Binary sort
- Insertion sort
- Merge sort
- Bubble sort
What is [5, 3, 8, 1] after one pass of bubble sort?
- [5, 3, 1, 8]
- [3, 5, 1, 8]
- [1, 3, 5, 8]
- [3, 5, 8, 1]
Binary search looks for 23 in [4, 8, 15, 16, 23, 42, 50]. Which item does it check first?
- 4
- 23
- 42
- 16
Which TWO sorts work in place, using very little extra memory?
Tick 2.
- Merge sort
- Bubble sort
- Insertion sort
Linear search only works on sorted lists.
- True
- False
Which algorithm splits a list into single items, then merges them back in order?
- Merge sort
- Bubble sort
- Linear search
- Insertion sort
Which search checks each item in turn from the start?
Quiz Challenge, round 2: Quick Fire
Explain why binary search is more efficient than linear search on a large sorted list. [2 marks]
Describe how insertion sort works. [2 marks]
Give one advantage and one disadvantage of merge sort compared with bubble sort. [2 marks]
Describe what happens during one pass of a bubble sort. [2 marks]
Quiz Challenge, round 3: Big Think
Find 23 in the list [4, 8, 15, 16, 23, 42, 50] using a binary search. Show each step. [4 marks]
A shop has a list of 2 million products, sorted by product code. Recommend a search algorithm and justify your choice. Then explain which sort you would use if the list were not sorted. [6 marks]
This or That: tick the right answer
Which search works on an unsorted list?
- Binary search
- Linear search
Which sort splits the list into single items first?
- Merge sort
- Insertion sort
Sorting the cards in your hand one at a time is most like...
- Merge sort
- Insertion sort
A device has very little memory. Which sort suits it?
- Bubble sort
- Merge sort
When does a bubble sort stop?
- After a pass with no swaps
- After exactly one pass
Binary search is used on an unsorted list. What do you get?
- Unreliable answers
- The right answer, just slower
Each comparison in a binary search removes...
- One item
- Half of the remaining items
Which sort stays fast however jumbled the list is?
- Bubble sort
- Merge sort
Memory Match: match each algorithm to its how it works
| Algorithm | How it works |
|---|---|
| A Linear search | 1 Splits into single items, then merges |
| B Binary search | 2 Places each item into a sorted section |
| C Bubble sort | 3 Sorts using almost no extra memory |
| D Insertion sort | 4 Checks each item in turn |
| E Merge sort | 5 Halves a sorted list each time |
| F In place | 6 Swaps adjacent pairs in the wrong order |
Sort It: tick one box in each row
Which algorithm does each statement describe?
| Statement | Binary search | Bubble sort | Merge sort | Insertion sort |
|---|---|---|---|---|
| Halves the search area with every comparison | ||||
| Splits the list until every list has one item | ||||
| Like sorting a hand of playing cards | ||||
| Fast and consistent on large, jumbled lists | ||||
| Uses extra memory to hold the sub-lists | ||||
| After the first pass, the largest value is at the end | ||||
| Checks the middle item first | ||||
| Builds a sorted section at the start of the list | ||||
| Only works on a sorted list | ||||
| Very efficient when the list is nearly sorted | ||||
| Compares adjacent pairs and swaps them | ||||
| Stops after a pass with no swaps |
Answers
Round 1: Quiz Blitz
- It must be sorted Binary search discards halves, which only works if the order tells you which half to keep.
- Compares adjacent items and swaps them if they are in the wrong order After each pass, the largest remaining value has bubbled to the end.
- True It stores the sub-lists separately. Bubble sort works in place.
- 50 In the worst case the item is last, or missing, so every item is checked.
- Find the middle item → If it is the target, stop → Discard the half that cannot contain the target → Repeat on the remaining half Each comparison removes half of the remaining items.
- Insertion sort Most items need almost no movement, so insertion sort is very fast here.
- [3, 5, 1, 8] Swap 5 and 3, leave 5 and 8, swap 8 and 1. The largest, 8, is now at the end.
- 16 The middle item. 23 is larger than 16, so the lower half is discarded.
- Bubble sort; Insertion sort Merge sort needs extra memory for its sub-lists.
- False Linear search works on any list. Binary search is the one that needs sorted data.
- Merge sort Divide, then merge: a list of one item is already sorted.
- linear search Linear search is simple, and works on any list, but is slow on large ones.
Round 2: Quick Fire
- Each comparison removes half of the remaining items
- Linear search removes only one item with each comparison
Model answerEach comparison in a binary search removes half of the remaining items, whereas a linear search checks each item in turn, removing only one at a time.
- It builds a sorted section at the start of the list
- Each next item is put into its correct place in that section, shifting larger items along
Model answerInsertion sort builds a sorted section at the start of the list. It takes the next unsorted item and inserts it into its correct position in the sorted section, shifting larger items along to make room.
- Advantage: much faster on large lists
- Disadvantage: uses more memory, and is more complex
Model answerAdvantage: merge sort is much faster on large lists. Disadvantage: it uses more memory, because the sub-lists must be stored separately.
- Each pair of adjacent items is compared
- They are swapped if in the wrong order, so the largest ends up at the end
Model answerEach pair of adjacent items is compared in turn, and swapped if they are in the wrong order. By the end of the pass, the largest remaining value has moved to the end.
Round 3: Big Think
- Compare with the middle item first
- 23 is larger, so discard the lower half
- The next middle item is 42: 23 is smaller, so discard the upper half
- Found on the third comparison
Model answerCheck the middle item, 16. 23 is larger, so discard the lower half, leaving 23, 42 and 50. The middle of those is 42. 23 is smaller, so discard the upper half, leaving 23. The middle item is now 23, so it is found, on the third comparison.
- Binary search
- Because the list is already sorted
- Each comparison halves the remaining items
- So about 21 comparisons, instead of up to 2 million
- Linear search could check every item, which is too slow
- If unsorted: merge sort
- Merge sort is fast on large lists
- But it needs more memory
Model answerI recommend binary search, because the list is already sorted by product code. Each comparison halves the remaining items, so about 21 comparisons are enough to find any product, whereas a linear search could check every item, up to 2 million comparisons. If the list were not sorted, I would use merge sort first, because it is much faster than bubble sort on large lists, although it uses more memory.
This or That
- Linear search Binary search needs sorted data. Linear search checks every item, so order does not matter.
- Merge sort Merge sort divides until each list has one item, then merges them in order.
- Insertion sort You slot each new card into place among the sorted ones.
- Bubble sort Bubble sort works in place. Merge sort needs memory for its sub-lists.
- After a pass with no swaps A pass with no swaps proves the list is in order.
- Unreliable answers Discarding halves only works when the order tells you which half to keep.
- Half of the remaining items That is why it is so much faster on large lists.
- Merge sort Merge sort's performance is consistent. Bubble sort slows down badly on jumbled lists.
Memory Match
- A 4 Linear search: Checks each item in turn
- B 5 Binary search: Halves a sorted list each time
- C 6 Bubble sort: Swaps adjacent pairs in the wrong order
- D 2 Insertion sort: Places each item into a sorted section
- E 1 Merge sort: Splits into single items, then merges
- F 3 In place: Sorts using almost no extra memory
Sort It
- Binary search Halves the search area with every comparison That is why binary search is so fast.
- Merge sort Splits the list until every list has one item Merge sort divides, then merges.
- Insertion sort Like sorting a hand of playing cards Each new card is slotted into place.
- Merge sort Fast and consistent on large, jumbled lists Merge sort's performance does not depend on how jumbled the list is.
- Merge sort Uses extra memory to hold the sub-lists Merge sort's speed costs memory.
- Bubble sort After the first pass, the largest value is at the end The largest value bubbles to the end on every pass.
- Binary search Checks the middle item first Binary search always compares with the middle.
- Insertion sort Builds a sorted section at the start of the list Insertion sort grows a sorted section.
- Binary search Only works on a sorted list Binary search needs order to know which half to discard.
- Insertion sort Very efficient when the list is nearly sorted Most items barely need to move.
- Bubble sort Compares adjacent pairs and swaps them That is how bubble sort works.
- Bubble sort Stops after a pass with no swaps No swaps means the list is sorted.