AQA 8525 GCSE Computer Science · 3.1 Fundamentals of Algorithms
Stretch tasks 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 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
Sort It
Sort each statement into the right box, like the tick-box tables in the exam.
- 12 cards to sort into 2 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: Sorting Algorithms
Sorting Algorithms: the questions
Quiz Challenge, round 1: Quiz Blitz
What does a bubble sort compare?
- The first and last items
- Items in two halves
- Each item with the middle item
- Adjacent pairs of items
When does a bubble sort stop?
- After n squared passes
- When a whole pass makes no swaps
- After exactly one pass
- When the middle item is found
What is the most number of passes a bubble sort needs to put 6 items in place?
What is [5, 3, 8, 1] after the second pass of a bubble sort?
- [3, 5, 1, 8]
- [3, 5, 8, 1]
- [3, 1, 5, 8]
- [1, 3, 5, 8]
Merge sort needs more memory than bubble sort.
- True
- False
Why does a swap need a temporary variable?
- To make the sort faster
- Without it, one of the two values would be overwritten
- To count the passes
- To store the length of the list
Merge sort keeps splitting the list until...
- It has been cut in half once
- Every sub-list has two items
- The middle item is found
- Every sub-list has one item
Which TWO are true of bubble sort?
Tick 2.
- It splits the list into halves
- It always needs n squared passes
- It sorts in place, using little extra memory
- It is simple to write
- It is fast on very large lists
What do you get by merging [3, 7, 9] with [1, 2, 5]?
- [1, 3, 2, 7, 5, 9]
- [9, 7, 5, 3, 2, 1]
- [1, 2, 3, 5, 7, 9]
- [3, 7, 9, 1, 2, 5]
Ten times more data makes a bubble sort take roughly...
- 100 times longer
- 10 times longer
- Twice as long
- The same time
On a list that is almost sorted already, a bubble sort can finish quickly.
- True
- False
Which strategy does merge sort use?
- Trial and error
- Brute force
- Abstraction
- Divide and conquer
Quiz Challenge, round 2: Quick Fire
Describe how a bubble sort works. [3 marks]
Describe how a merge sort works. [3 marks]
Give one advantage of bubble sort and one advantage of merge sort. [2 marks]
Explain why a bubble sort is slow on large lists. [2 marks]
Quiz Challenge, round 3: Big Think
Compare bubble sort and merge sort. [6 marks]
A bubble sort is used on [5, 3, 8, 1]. Describe what happens in the first pass, and how the algorithm knows when to stop. [4 marks]
This or That: tick the right answer
Which sorts in place?
- Merge sort
- Bubble sort
Which is usually written recursively?
- Bubble sort
- Merge sort
At the end of merge sort's splitting, how many items does each sub-list hold?
- Two
- One
A question asks for each pass. What should you show?
- The list after each pass
- The list after each swap
Merging two sorted lists, which item do you take next?
- The smaller of the two front items
- The larger of the two front items
Is bubble sort useless?
- Yes: never use it
- No: it suits small, nearly sorted lists
Doubling the data for a merge sort roughly...
- Quadruples the work
- Doubles the work
What does the swapped flag in a bubble sort track?
- Whether this pass made any swap
- How many items are sorted
Memory Match: match each term to its meaning
| Term | Meaning |
|---|---|
| A Bubble sort | 1 Splits to single items, then merges |
| B Merge sort | 2 Exchanging two adjacent items |
| C Pass | 3 Holds a value safe during a swap |
| D Swap | 4 Sorting with almost no extra memory |
| E Temporary variable | 5 Swaps adjacent items in the wrong order |
| F In place | 6 One complete run through the list |
Sort It: tick one box in each row
Bubble sort or merge sort?
| Statement | Bubble sort | Merge sort |
|---|---|---|
| Good for very small or nearly sorted lists | ||
| Good for large lists where speed matters | ||
| Sorts in place | ||
| Uses a temporary variable to swap | ||
| Stops when a pass makes no swaps | ||
| Compares adjacent items | ||
| Roughly n squared comparisons | ||
| Needs extra memory for the sub-lists | ||
| Uses divide and conquer | ||
| Splits the list until each part has one item | ||
| Roughly n log n comparisons | ||
| Usually written recursively |
Answers
Round 1: Quiz Blitz
- Adjacent pairs of items It swaps each adjacent pair that is in the wrong order.
- When a whole pass makes no swaps A pass with no swaps proves the list is in order.
- 5 At most n minus 1: once 5 items are in place, the last one has nowhere else to be.
- [3, 1, 5, 8] Pass 1 gives [3, 5, 1, 8]. Pass 2 swaps 5 and 1, giving [3, 1, 5, 8].
- True It holds the sub-lists in memory while merging. Bubble sort works in place.
- Without it, one of the two values would be overwritten temp ← a, a ← b, b ← temp keeps both values safe.
- Every sub-list has one item A list of one item is already sorted, so all the work happens in the merging.
- It sorts in place, using little extra memory; It is simple to write Simple and memory-light, but slow on large lists.
- [1, 2, 3, 5, 7, 9] Keep taking the smaller of the two front items.
- 100 times longer Bubble sort does roughly n squared comparisons, so 10 x 10 = 100.
- True A pass with no swaps comes early, and the sort stops.
- Divide and conquer Split the problem, solve the small parts, combine the results.
Round 2: Quick Fire
- It compares adjacent pairs of items
- It swaps them if they are in the wrong order
- After each pass the largest remaining item is at the end
- It stops when a pass makes no swaps
Model answerA bubble sort compares each pair of adjacent items and swaps them if they are in the wrong order. After each pass, the largest remaining item has moved to the end. It stops when a pass is completed with no swaps.
- It splits the list in half, again and again
- Until every sub-list holds a single item
- Then sub-lists are combined back together in order
- By repeatedly taking the smaller of the two front items
Model answerIt splits the list in half, then halves each half, until every sub-list holds a single item. Then pairs of sub-lists are combined back together in order, by repeatedly comparing the front items and taking the smaller.
- Bubble sort: little memory, and simple to write
- Merge sort: much faster on large lists
Model answerBubble sort uses very little memory, because it sorts in place. Merge sort is much faster on large lists.
- It may need many passes, comparing every adjacent pair each time
- About n squared comparisons, so ten times the data is a hundred times the work
Model answerIt may need many passes, each comparing every adjacent pair, so it does roughly n squared comparisons: ten times as much data means about a hundred times as much work.
Round 3: Big Think
- Bubble sort swaps adjacent items in the wrong order
- It stops when a pass makes no swaps
- Merge sort splits into single items, then puts them back in order
- Merge sort is far faster on large lists
- Bubble sort needs roughly n squared comparisons
- Bubble sort sorts in place, using little memory
- Merge sort needs extra memory for the sub-lists
- Bubble sort is simpler; merge sort is more complex
- The best choice depends on list size and memory
Model answerBubble sort repeatedly compares adjacent items and swaps them if they are in the wrong order, stopping when a pass makes no swaps. Merge sort splits the list into halves until every sub-list has one item, then merges them back in order. Merge sort is far faster on large lists, because it halves the problem, whereas bubble sort needs roughly n squared comparisons. But bubble sort sorts in place, using very little memory, while merge sort needs extra memory to hold the sub-lists, and it is more complex to write. Which is better depends on the size of the list and the memory available.
- 5 and 3 are compared and swapped
- 5 and 8 are compared and not swapped
- 8 ends up in its final position at the end
- It stops when a whole pass makes no swaps
Model answer5 and 3 are compared and swapped. 5 and 8 are compared and not swapped. 8 and 1 are compared and swapped, giving [3, 5, 1, 8], so 8, the largest value, is now in its final position at the end. The algorithm keeps making passes, and stops when a pass is completed with no swaps.
This or That
- Bubble sort Bubble sort swaps items inside the original list. Merge sort builds sub-lists.
- Merge sort Merge sort calls itself on each half.
- One A one-item list is already sorted, which is what makes the method work.
- The list after each pass Showing every swap when passes were asked for loses marks.
- The smaller of the two front items Taking the smaller front item each time keeps the result in order.
- No: it suits small, nearly sorted lists On a short or almost sorted list it finishes quickly, with no extra memory.
- Doubles the work About n log n comparisons: far better than bubble sort's n squared.
- Whether this pass made any swap If a pass ends with swapped still False, the list is sorted.
Memory Match
- A 5 Bubble sort: Swaps adjacent items in the wrong order
- B 1 Merge sort: Splits to single items, then merges
- C 6 Pass: One complete run through the list
- D 2 Swap: Exchanging two adjacent items
- E 3 Temporary variable: Holds a value safe during a swap
- F 4 In place: Sorting with almost no extra memory
Sort It
- Bubble sort Good for very small or nearly sorted lists A pass with no swaps comes quickly.
- Merge sort Good for large lists where speed matters Doubling the data only roughly doubles the work.
- Bubble sort Sorts in place It needs almost no extra memory.
- Bubble sort Uses a temporary variable to swap Swapping two items needs somewhere to hold one.
- Bubble sort Stops when a pass makes no swaps That is bubble sort's stopping condition.
- Bubble sort Compares adjacent items Bubble sort works through adjacent pairs.
- Bubble sort Roughly n squared comparisons That is why it is slow on large lists.
- Merge sort Needs extra memory for the sub-lists The sub-lists are held while merging.
- Merge sort Uses divide and conquer Split, solve, combine.
- Merge sort Splits the list until each part has one item Merge sort divides, then merges.
- Merge sort Roughly n log n comparisons That is why it is fast on large lists.
- Merge sort Usually written recursively Each half is sorted by the same routine.