Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1pivot = items[0] | AThe pivot is missing from the result, so it is lost. It should be [pivot] between the two sorted parts. | |
| 2smaller = [x for x in items[1:] if x < pivot] | Bif len(items) <= 1: return items | |
| 3Divide and conquer: sort each part with the same method, then join the parts around the pivot | CThe pivot is still inside items, so the loop includes it. It can be duplicated or cause endless recursion. Use items[1:] so the pivot is left out. | |
| 4Base case: a list with 0 or 1 items is already sorted, so the recursion stops | DPartition step: collect the items that are smaller than the pivot into their own list | |
| 5smaller = [x for x in items if x < pivot] larger = [x for x in items if x > pivot] | EChoose the first item in the list as the pivot that the other items are compared against | |
| 6return quick_sort(smaller) + quick_sort(larger) | Freturn quick_sort(smaller) + [pivot] + quick_sort(larger) |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
quick sort — Picks a pivot, splits the list into items smaller and larger than the pivot, then sorts each part the same way (recursion) until the parts are sorted.pivot = items[0]smaller = [x for x in rest if x < pivot]right = quick_sort(larger)if len(items) <= 1:larger = [x for x in rest if x >= pivot]