34.5 Exercises
Factual
- Which of the following function(s) have the slowest order of growth in terms of Big Theta?
- To solve puppy, cat, dog for 12 items, what is the theoretical minimum number of comparisons we have to make, based on the argument used in lecture? Please round your answer up to the nearest whole number.
- Which of the following statements are true?
- The optimal sorting algorithm takes at most time.
- It is impossible to find a comparison-based sorting algorithm that takes less than comparisons.
- It is impossible to find a comparison-based sorting algorithm that takes less than time.
- It is impossible to find a sorting algorithm that takes less than time.
Problem 1
In lecture, we proved that . Thus, both and have the same order of growth, and are slower than or .
Problem 2
, based on the equation we saw in lecture.
Problem 3
- The optimal sorting algorithm takes at most time. We know of several sorts (for example, mergesort) that take time in the worst case. So the hypothetical “best” sorting algorithm cannot be worse than this.
- It is impossible to find a comparison-based sorting algorithm that takes less than comparisons. This is the comparison sorting bound proved in lecture.
- It is impossible to find a comparison-based sorting algorithm that takes less than time. Each comparison must take at least constant time, so the time complexity of comparison-based sorts has the same lower bound as the number of comparisons.
- It is impossible to find a sorting algorithm that takes less than time. The lower bound proved in lecture only applies to comparison-based sorts. We will see in future lectures how to use non-comparison sorts to reduce this runtime even further.
Metacognitive
- Suppose we add a new method to
Arrays.sortthat takes in an array of strings. What algorithm shouldArrays.sort(String[] x)use?
Problem 1
Quicksort. We don’t need stability, since strings are only equal by .equals if they are exactly the same. Quicksort, then, is the best algorithm since it is empirically the fastest.