30.5 Exercises
Last updated
Last updated
Suppose we have a sorted array in Java. What is the best approach for finding a particular item in the array: binary search or linear iteration?
Repeat the above exercise for a sorted array stored on physical tape.
Given the array [90, 50, 20, 30, 40, 10, 60, 50, 30]
, what would be the best pivot?
If we have 10 items to sort, how many compares will it take to selection sort all of the items?
What is the depth of the quicksort recursive tree in the best and worst case? Give an exact answer, not a theta bound.
Assume that we always pick the leftmost item as our pivot for quicksort. Identify the three types of arrays that will cause a worst-case runtime. For partitioning, assume that we partition into two arrays: items less than or equal to the pivot, and items greater than the pivot.
In the worst case, we make recursive calls if we always choose the smallest or largest item as our pivot. Thus, the recursive tree has depth .
In the best case, we always choose the median as our pivot. This halves the size of the array at each level, resulting in levels.