Skip to content
The Lumenary

Practice · Grades 9–12

Algorithms, correctness and efficiency

Algorithms, correctness and efficiency

Warm-upHuman

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

Algorithms, correctness and efficiency

Warm-upHuman

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

Algorithms, correctness and efficiency

CoreHuman

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?

Choose one

Algorithms, correctness and efficiency

CoreHuman

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

Choose one

Algorithms, correctness and efficiency

CoreHuman

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?

Algorithms, correctness and efficiency

CoreHuman

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

Choose one

Algorithms, correctness and efficiency

CoreFormal

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

Choose one

Algorithms, correctness and efficiency

CoreComputational

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.

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

Algorithms, correctness and efficiency

StretchHuman

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?

Algorithms, correctness and efficiency

StretchHuman

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).)