DSC 40B

Problems tagged with "quicksort"

Problem #143

Tags: lecture-07, loop invariants, quicksort

Consider the following code, which takes in a list of integers and moves all of the odd numbers to the front and all of the even numbers to the end, in-place.

Remember that the \(\%\) operator computes the remainder of division. For example \(5 \% 2 = 1\) and \(4 \% 2 = 0\). This can be used to check if a number is even; if \(x \% 2 = 0\), then x is even.

def separate_odd_from_even(numbers):
    """Moves the odd numbers to the front, in-place."""
    n = len(numbers)

    def swap(i, j):
        numbers[i], numbers[j] = numbers[j], numbers[i]

    odd_barrier = 0
    even_barrier = n - 1

    while odd_barrier < even_barrier:
        if numbers[odd_barrier] % 2 == 0:
            # the number is even
            swap(odd_barrier, even_barrier)
            even_barrier -= 1
        else:
            # the number is odd
            odd_barrier += 1

Part 1)

Which of the following are valid loop invariants for the while-loop? Select all that apply.

Solution

3rd option: After each iteration, the elements in numbers[:odd_barrier] are odd.

6th option: After each iteration, the elements in numbers[even_barrier+1:] are even.

8th option: After the \(\alpha\)th iteration, even_barrier - odd_barrier = n - 1 - \(\alpha\).

Part 2)

How many iterations will the while-loop in separate_odd_from_even make if numbers has length \(100\)? Your answer should be a number.

Solution

\(99\).

Each iteration either increments odd_barrier or decrements even_barrier, so even_barrier - odd_barrier goes down by exactly 1 per iteration. It starts at \(n - 1 = 99\) and the loop stops when it reaches 0, after 99 iterations.

Problem #144

Tags: lecture-07, loop invariants, quicksort

Consider this code which partitions a list in a special way:

def is_even(i):
    """Returns True if i is even, False otherwise."""
    return i % 2 == 0

def mystery_partition(numbers):
    def swap(i, j):
        numbers[i], numbers[j] = numbers[j], numbers[i]

    barrier_ix = 0
    for i in range(len(numbers)):
        if is_even(numbers[i]):
            swap(i, barrier_ix)
            if numbers[barrier_ix] > numbers[0]:
                swap(0, barrier_ix)
            barrier_ix += 1

Which of the following loop invariants are true for this code? Select all that apply.

Note: any statement about an empty set or list is considered to be automatically true.

Solution

1st option: After each iteration of the for loop, all numbers in numbers[:barrier_ix] are even.

3rd option: After each iteration of the for loop, numbers[0] is greater than or equal to all numbers in numbers[:barrier_ix].

Problem #150

Tags: lecture-08, quicksort

What are the expected, best case, and worst case time complexities of quicksort?

Solution

Expected time complexity is \(\Theta(n \log n)\), best case time complexity is \(\Theta(n \log n)\), and worst case time complexity is \(\Theta(n^2)\).

In the best case, every pivot splits the array in half, giving the recurrence \(T(n) = 2T(n/2) + \Theta(n)\) (the same as mergesort), whose solution is \(\Theta(n \log n)\). In the worst case, every pivot is the minimum or maximum, giving \(T(n) = T(n-1) + \Theta(n)\), whose solution is \(\Theta(n^2)\).