Exercises - Lecture 6

6A

  1. Partition the following array on each of the following pivots. Your answer doesn’t need to be the exact same one my pseudocode would produce (and therefore multiple correct answers are possible), but it does need to satisfy the postcondition of partition.

    [5, 4, 8, 2, 3, 6, 9, 1]

    1. Partition on the element with value 4

    2. Partition on the element with value 1

  2. Execute quicksort on the following array. Use the first element as the pivot. Write the state of the array after each full level of subdivision is partitioned (i.e., after the first partition, after the partition of both parts, and so on). Once again, this is out of order from how it would be executed recursively and that’s fine; we’re only going for a conceptual understanding of the steps here.

    A = new int[]{5, 4, 8, 2, 3, 6, 9, 1}

6B

  1. What is the runtime of quicksort on a sorted array if you choose the pivot to be

    1. the first element?

    2. the last element?

    3. a random element?

6C

  1. You are tasked with sorting a list of words alphabetically by their first letter only; for example, “wow” and “whee” are considered equivalent because they have the same first letter. Sort the following array stably on the first character of each word.

    ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight" ]

  2. What is the space complexity of mergesort?