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 | |
|---|---|---|
| 1define quickSort(array) | AThe pivot is left out of the result, so it is lost from the sorted array. It should be quickSort(smaller) + [pivot] + quickSort(larger). | |
| 2quickSort(smaller) | Bif item < pivot then add item to smaller | |
| 3Partition step: values smaller than the pivot are placed in a separate array | CThe function header: it names the function quickSort and takes the array to be sorted as a parameter | |
| 4Base case: an array with 0 or 1 items is already sorted, so the function stops calling itself | DA recursive call that sorts the part of the array holding values less than the pivot | |
| 5define quickSort(array) pivot <- array[0] smaller <- quickSort(smaller) larger <- quickSort(larger) | EThere is no base case, so the function never stops calling itself and the recursion is infinite. It also calls quickSort on arrays that were never partitioned. Add a check for length <= 1 and partition before the calls. | |
| 6return quickSort(smaller) + quickSort(larger) | Fif length(array) <= 1 then return array |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
quickSort(array) — A recursive function that picks a pivot, partitions the array into smaller and larger parts around it, then calls itself on each part until the array is sorted.define quickSort(array)if length(array) <= 1 then
return arraypivot ← array[0]for each item in rest
if item < pivot then
add item to smallerleft ← quickSort(smaller)
right ← quickSort(larger)
return left + [pivot] + right