Algorithms, correctness and efficiency
Warm-upHumanWhat is the largest number of comparisons binary search can make on a sorted list of entries?
What is the largest number of comparisons binary search can make on a sorted list of entries?
How many comparisons does insertion sort make, in the worst case, to sort a list of entries?
This loop adds up a list of length :
s = 0
for i in range(n): # i = 0, 1, ..., n - 1
s = s + a[i]
Which statement is a loop invariant that holds at the start of each pass (before is added) and proves that is the total at the end?
A classmate's sorting function passed random tests on lists of up to numbers. Which statement is correct?
An algorithm takes steps on inputs of size . A new computer is times faster. In the time the old computer needed for size , the new one can handle size . What is ?
In the correctness proof of binary search, suppose . Why may we discard the indices ?
Let r := bsearch p lo hi. Which Lean proposition says that r is the first point where the test p gives true?
Write binary_search(a, t) for a sorted list a. Return a pair (index, probes): an index i with a[i] == t (or -1 if t is absent), and the number of times you compared t with an entry of the list. The tests check correctness on many lists and that probes never exceeds , which is len(a).bit_length() in Python.
Using the decision-tree argument (Theorem 6), what is the smallest number of comparisons that any comparison-based method could need, in the worst case, to sort different numbers?
Run Euclid's algorithm on . How many divisions with remainder does it perform before reaching a pair whose second number is ? (Count every step .)