Skip to content
The Lumenary

Each worksheet prints cleanly on A4 or Letter. Answer keys are on a separate page.

The Lumenary · Grades 9–12

Algorithms, correctness and efficiency

Name: ______________________

Date: ______________________

You will learn to

  • Implement linear search, binary search and insertion sort, and trace them on examples
  • Prove an algorithm correct with a loop invariant and a decreasing measure, for binary search and for Euclid's algorithm
  • Prove that binary search on nn items makes at most ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1 comparisons, and that insertion sort makes at most n(n−1)/2n(n-1)/2
  • Prove that every comparison-based sorting method needs at least log⁡2(n!)\log_2(n!) comparisons in the worst case, which grows like nlog⁡2nn \log_2 n
  • Compare growth rates such as log⁡n\log n, nn, nlog⁡nn \log n, n2n^2 and 2n2^n, and explain why faster computers do not make efficiency irrelevant
  1. 1.

    What is the largest number of comparisons binary search can make on a sorted list of 10001000 entries?

  2. 2.

    How many comparisons does insertion sort make, in the worst case, to sort a list of 1010 entries?

  3. 3.

    This loop adds up a list aa of length nn:

    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 aia_i is added) and proves that ss is the total at the end?

    • A.s=a0+a1+⋯+ai−1s = a_0 + a_1 + \dots + a_{i-1}
    • B.s=a0+a1+⋯+ais = a_0 + a_1 + \dots + a_i
    • C.s≥0s \ge 0
    • D.s=a0+a1+⋯+an−1s = a_0 + a_1 + \dots + a_{n-1}
  4. 4.

    A classmate's sorting function passed 10001000 random tests on lists of up to 5050 numbers. Which statement is correct?

    • A.This is evidence that the function works, but it does not prove it works on every list.
    • B.It is now proved correct for all lists of length at most 5050.
    • C.It is proved correct, since 10001000 is a large number of tests.
    • D.The tests are useless; only proofs matter.
  5. 5.

    An algorithm takes n2n^2 steps on inputs of size nn. A new computer is 100100 times faster. In the time the old computer needed for size nn, the new one can handle size k⋅nk \cdot n. What is kk?

  6. 6.

    In the correctness proof of binary search, suppose amid<ta_{mid} < t. Why may we discard the indices lo,…,midlo, \dots, mid?

    • A.The list is sorted, so ai≤amid<ta_i \le a_{mid} < t for every i≤midi \le mid: none of these entries equals tt.
    • B.Because midmid is the middle, so tt is equally likely to be in either half.
    • C.Because we have already compared tt with all of alo,…,amida_{lo}, \dots, a_{mid}.
    • D.Because lo≤mid≤hilo \le mid \le hi.
  7. 7.

    Let r := bsearch p lo hi. Which Lean proposition says that r is the first point where the test p gives true?

    • A.p r = true ∧ ∀ k, k < r → p k = false
    • B.p r = true
    • C.∃ k, p k = true
    • D.∀ k, k < r → p k = true
  8. 8.

    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 ⌊log⁡2n⌋+1\lfloor \log_2 n \rfloor + 1, which is len(a).bit_length() in Python.

    Computational — Practice

  9. 9.

    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 55 different numbers?

  10. 10.

    Run Euclid's algorithm on (89,55)(89, 55). How many divisions with remainder does it perform before reaching a pair whose second number is 00? (Count every step (a,b)→(b,a mod b)(a, b) \to (b, a \bmod b).)

Code: Apache-2.0 · Content: CC BY-SA 4.0