Algorithms and complexity · A level · OCR H446 2.3.1, Eduqas A500QS 1.3 · about 25 min
Pivots and in-place partitioning, O(n log n) on average and O(n^2) at worst, and the four sorts compared.
[1 mark]What is the worst-case time complexity of quick sort?
[1 mark]Quick sort always uses the last item as its pivot. Which list makes it slowest?
[1 mark]Which are true of quick sort?
Tick every answer that is true.
[1 mark]This partitions the list around its last item. What does it print?
items = [7, 2, 9, 4, 5]
pivot = items[-1]
i = 0
for j in range(len(items) - 1):
if items[j] < pivot:
items[i], items[j] = items[j], items[i]
i = i + 1
items[i], items[-1] = items[-1], items[i]
print(items, i)[1 mark]How does quick sort differ from merge sort in where it does its work?
Write quick_sort(items, low, high) using the in-place partition from this lesson: the pivot is items[high], items strictly less than the pivot go to its left, and the function sorts the left part before the right part. It sorts items between indexes low and high inclusive, and returns the number of comparisons with a pivot that it made.
Each time a partition puts a pivot in its final place, print pivot <value> placed at index <index>. Parts of fewer than 2 items are not partitioned and print nothing. Then print, in this order:
- sorted: followed by readings after sorting
- readings: <n> comparisons
- ordered: <n> comparisons, sorting ordered, which is already in order (print no pivot lines for this one: give the function a way to switch printing off, such as a parameter show that defaults to True)
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
readings = [38, 12, 71, 45, 9, 83, 27, 60, 50]
ordered = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
def quick_sort(items, low, high):
return 0Plan your program here, then type it in and press Run.
[5, 5, 5, 5, 5]. What happens, and why is it slow?(name, time) records that quick sort is not stable.