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 | |
|---|---|---|
| 1for item in data: if item == target: | ABinary search: halves the search range each step, so it needs very few steps but the data must be sorted | |
| 2mid = (low + high) // 2 | BLinear search: checks items one at a time, so up to n steps for n items, and works on unsorted data | |
| 3Selection sort takes about n x n steps in every case, because it scans the unsorted part to find the minimum for each position | Cfor i in range(n): for j in range(i + 1, n): | |
| 4Quick sort is usually much faster on large data because it splits the list around a pivot, but its worst case (a bad pivot) is slow | DBinary search is used on unsorted data. It only works correctly if the list is sorted first, so sort it or use linear search. | |
| 5data = [9, 2, 7, 4] binary_search(data, 7) | EThe best case for linear search is when the target is the first item (1 step). The last item (or a missing target) is the worst case, needing n steps. | |
| 6# Best case for linear search: # target is the last item | Fleft = [x for x in data if x < pivot] |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
algorithm efficiency — How many steps an algorithm takes as data grows, and what it needs (e.g. sorted data for binary search). Compare it to choose between linear or binary search, selection or quick sort.data = [9, 2, 7, 4]
data = sorted(data)
index = binary_search(data, 7)worst = len(data)
print(worst)best = 1
print(best)n = len(data)
steps = n ** 2if data == sorted(data):
index = binary_search(data, 7)
else: index = linear_search(data, 7)