Lesson 7.7 · Trace it
Trace: binary search
Python code10 rows to fill4 marks
Complete the trace table for this Python code. The values are sorted, so this is a binary search. // divides and throws away the remainder. Then answer the question below the table. [4]
Line 1: values = [4, 9, 13, 18, 25, 31, 40, 47]Line 2: target = 13Line 3: low = 0Line 4: high = 7Line 5: found = FalseLine 6: while low <= high and not found:Line 7, indented 1 level: mid = (low + high) // 2Line 8, indented 1 level: if values[mid] == target:Line 9, indented 2 levels: found = TrueLine 10, indented 1 level: elif values[mid] < target:Line 11, indented 2 levels: low = mid + 1Line 12, indented 1 level: else:Line 13, indented 2 levels: high = mid - 1Line 14: print(mid)
Fill in one row each time a line changes low, high, mid or found, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.
| Row | low | high | mid | found | output |
|---|---|---|---|---|---|
| 1 | |||||
| 2 | |||||
| 3 | |||||
| 4 | |||||
| 5 | |||||
| 6 | |||||
| 7 | |||||
| 8 | |||||
| 9 | |||||
| 10 |