Python · A Level 9
Sorting Algorithms in Depth
Name
Class
Date
Mark / 30
Section A
Circle the letter of the correct answer. One mark each.
- 1What is the pivot in a quick sort?1
- AThe last item that is placed in order when the sort is complete
- BThe smallest item in the list, which is always moved to the front
- CThe item in the exact middle of the list, which is never moved
- DThe item that the other items are compared with when the list is partitioned
- 2What is the average time complexity of quick sort?1
- AO(n)
- BO(n squared)
- CO(n log n)
- DO(log n)
- 3When is the worst case of quick sort with the first item as pivot?1
- AWhen the list has exactly two items
- BWhen the list is in random order
- CWhen the list has only one item
- DWhen the list is already sorted
- 4What is the worst case time complexity of merge sort?1
- AO(n log n)
- BO(2 to the power n)
- CO(n squared)
- DO(n)
- 5Which of these sorts needs O(n) extra memory?1
- ABubble sort
- BInsertion sort
- CMerge sort
- DIn place quick sort
- 6What does it mean for a sort to be stable?1
- AIt never crashes on invalid data
- BIt does not use any extra memory
- CItems with equal keys stay in their original order
- DIt always takes the same time whatever the data is
- 7Which sort needs the fewest comparisons on a list that is already sorted?1
- AQuick sort with the first item as pivot
- BInsertion sort, with n - 1 comparisons
- CMerge sort, with exactly one comparison
- DAll of them need the same number
- 8Why do real quick sort implementations choose the pivot randomly or by median of three?1
- ATo make the list fit in the cache
- BTo make the sort stable
- CTo reduce the amount of memory used
- DTo avoid the worst case on data that is already in order
- 9Which algorithm does Python's built in
sorteduse?1- ATimsort, a hybrid of merge sort and insertion sort
- BSelection sort
- CBubble sort
- DQuick sort with the first item as pivot
- 10A list of records is sorted by name, then the sort is repeated with grade as the key using a stable sort. What is the result?1
- ASorted by name, with the grades ignored completely
- BSorted by grade, with records of the same grade still in name order
- CAn error, because a list cannot be sorted twice
- DSorted by grade, with records of the same grade in a random order
Section B
Answer in the space provided. The number of marks tells you how many separate points to make.
- 1DescribeDescribe how a quick sort works.4
- 2StateA quick sort uses the first item of the list as its pivot. State the two lists that are formed, and the position of the pivot, when the list [6, 3, 9, 1, 8, 4] is partitioned.3
- 3WriteWrite a recursive Python function called quick_sort that takes a list and returns a new list with the same items in ascending order. It must use the first item as the pivot.4
- 4CompareCompare the merge sort and quick sort algorithms.6
- 5ExplainA list of student records is sorted by name, and is then sorted again by grade using a stable sort. Explain what is meant by a stable sort, and what the order of the final list will be.3
Mark scheme
Section A
Section B
1 Describe how a quick sort works. 4
One mark for each of the following, to a maximum of 4.
- An item is chosen as the pivot
- The list is partitioned so that items smaller than the pivot are on one side and larger items on the other
- The pivot is then in its final position
- The same process is repeated recursively on each part until the parts have one item or none
Model answer. An item is chosen as the pivot. The list is partitioned, so that all the items smaller than the pivot are on one side of it and all the larger items on the other, which puts the pivot in its final position. The same process is then repeated recursively on each of the two parts, until each part has one item or none, at which point the whole list is sorted.
2 A quick sort uses the first item of the list as its pivot. State the two lists that are formed, and the position of the pivot, when the list [6, 3, 9, 1, 8, 4] is partitioned. 3
One mark for each of the following, to a maximum of 3.
- The items smaller than the pivot are 3, 1 and 4
- The items larger than the pivot are 9 and 8
- The pivot, 6, is placed between them, in position 3
Model answer. The pivot is 6. The items smaller than 6, in their original order, are 3, 1 and 4, and the items larger are 9 and 8. The pivot is placed between the two lists, so the arrangement is 3, 1, 4, then 6, then 9, 8, and 6 is now in its final position, index 3.
3 Write a recursive Python function called quick_sort that takes a list and returns a new list with the same items in ascending order. It must use the first item as the pivot. 4
One mark for each of the following, to a maximum of 4.
- Base case: a list of 0 or 1 items is returned as it is
- The first item is chosen as the pivot and the rest are split into smaller and larger lists
- The function calls itself on both the smaller and the larger list
- The results are joined with the pivot between them
Model answer.
def quick_sort(items):
if len(items) <= 1:
return items
pivot = items[0]
smaller = [x for x in items[1:] if x < pivot]
larger = [x for x in items[1:] if x >= pivot]
return quick_sort(smaller) + [pivot] + quick_sort(larger)
print(quick_sort([6, 3, 9, 1, 8, 4]))A list of 0 or 1 items is already sorted, which is the base case. Otherwise the first item is the pivot, the others are split into those smaller than it and the rest, each part is sorted by a recursive call, and the pivot is placed between the two sorted results. The call shown prints [1, 3, 4, 6, 8, 9].
4 Compare the merge sort and quick sort algorithms. 6
One mark for each of the following, to a maximum of 6.
- Both use divide and conquer, and both are O(n log n) on average
- The worst case of merge sort is still O(n log n)
- The worst case of quick sort is O(n squared), for example with a poor pivot on sorted data
- Merge sort needs O(n) extra memory for the merged lists, while quick sort can be done in place
- Merge sort is stable, but the usual in place quick sort is not
- A conclusion, such as quick sort being faster in practice for memory held data, but merge sort being better when the worst case must be avoided or a stable sort is needed
Model answer. Both are divide and conquer algorithms and both take O(n log n) time on average. The worst case of merge sort is still O(n log n), because it always splits into halves, whereas the worst case of quick sort is O(n squared), which happens when the pivot is repeatedly the smallest or largest item, as with the first item as pivot on data that is already sorted. Merge sort needs O(n) extra memory for the merged lists, while quick sort can be done in place and needs little extra memory. Merge sort is stable, so equal items stay in order, and the usual in place quick sort is not. Quick sort is often faster in practice for data held in memory, so it suits general use, but merge sort is the better choice when the worst case must be avoided, when the sort must be stable or when the data is too big to hold in memory.
5 A list of student records is sorted by name, and is then sorted again by grade using a stable sort. Explain what is meant by a stable sort, and what the order of the final list will be. 3
One mark for each of the following, to a maximum of 3.
- A stable sort keeps items with equal keys in their original relative order
- The final list is in order of grade
- Students with the same grade remain in alphabetical order of name
Model answer. A stable sort keeps items that have equal keys in the same relative order that they had before the sort. After the second sort the list is in order of grade, and students with the same grade are still in alphabetical order of name, because they were in that order before the second sort and the stable sort did not change it.