Quick sort: pivot and partitioning

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Quick sort: pivot and partitioning

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. In quick sort, what is the pivot?
A A chosen item the other items are compared against
B The smallest item in the list, always
2. After partitioning around the pivot, where do the items go?
A Smaller items to one side, larger items to the other side
B All items stay in their original order
3. Quick sort is an example of divide and conquer. What does this mean?
A Compare every item with every other item one at a time
B Split the problem into smaller parts and solve each part the same way
4. List: [5, 2, 8, 1, 9]. If the first item is the pivot, which items go in the 'smaller' part?
A 8 and 9
B 2 and 1
5. When does quick sort stop splitting a part?
A When the part has 0 or 1 items
B When the pivot is the largest item

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight 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)

Definitions

quick sort

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
pivot = items[_]
2.
smaller = [x for x in rest if x < _____]
3.
right = __________(larger)
4.
if ___(items) <= 1:
5.
larger = [x for x in ____ if x >= pivot]

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A pivot = items
B smaller = []
C larger = []
D for x in items[1:]:
E if x < pivot:
F smaller.append(x)
Question 2
A def qs(a):
B if len(a) >= 1:
C return a
D p = a[0]
E lo, hi = split(a[1:], p)
F return qs(lo)+[p]+qs(hi)
Question 3
A define choose_pivot(a):
B mid = len(a) // 2
C return a[mid]
Question 4
A def partition(a, p):
B smaller = []
C larger = 0
D for x in a:
E if x < p:
F smaller.append(x)
Question 5
A pivot = items[0]
B smaller = larger = []
C for x in items[1:]:
D if x < pivot:
E smaller.append(x)
F else: larger.append(x)

Answer Key

Quick sort: pivot and partitioning
Section A: Quick Questions
Q1
A — A chosen item the other items are compared against
Q2
A — Smaller items to one side, larger items to the other side
Q3
B — Split the problem into smaller parts and solve each part the same way
Q4
B — 2 and 1
Q5
A — When the part has 0 or 1 items
Section B: Match the Pairs
Q1
pivot = items[0] → Choose the first item in the list as the pivot that the other items are compared against
Q2
smaller = [x for x in items[1:] if x < pivot] → Partition step: collect the items that are smaller than the pivot into their own list
Q3
Divide and conquer: sort each part with the same method, then join the parts around the pivot → return quick_sort(smaller) + [pivot] + quick_sort(larger)
Q4
Base case: a list with 0 or 1 items is already sorted, so the recursion stops → if len(items) <= 1: return items
Q5
smaller = [x for x in items if x < pivot] larger = [x for x in items if x > pivot] → The 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.
Q6
return quick_sort(smaller) + quick_sort(larger) → The pivot is missing from the result, so it is lost. It should be [pivot] between the two sorted parts.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
pivot = items[0]
Q2
smaller = [x for x in rest if x < pivot]
Q3
right = quick_sort(larger)
Q4
if len(items) <= 1:
Q5
larger = [x for x in rest if x >= pivot]
Section E: Spot the Error
Q1
A — The pivot must be one item from the list, so it should be pivot = items[0]. Writing pivot = items makes the pivot the whole list, so it can't be compared with single items.
Q2
B — The base case must stop when the list has 0 or 1 items, so it should be if len(a) <= 1:. With >= almost every list returns early without being sorted, and an empty list never stops the recursion.
Q3
A — Python functions are started with the keyword def, not define. The line should be def choose_pivot(a):.
Q4
C — larger must start as an empty list so items can be added with .append(). It should be larger = []. A number has no append method.
Q5
B — This makes smaller and larger the same list, so items from both sides get mixed together. They need two separate lists: smaller, larger = [], [].