DSC 40B – Theoretical Foundations of Data Science II
Problems tagged with "quickselect"
Problem #048
Tags: quickselect
True or False: quickselect
requires the input array to be sorted in order to function correctly.
Solution
False.
Problem #075
Tags: loop invariants, quickselect
Recall the partition operation from quickselect
. Which of the following arrays could have been partitioned at least once? Select all that apply.
Solution
The second, third, and last all should be selected.
Problem #086
Tags: quickselect
True or False: in order to guarantee that its output is correct, quickselect
must be called on a sorted list.
Solution
False.
Problem #183
Tags: quickselect
The code for quickselect
is shown below. It is the same as the code provided in lecture, except that it contains an added print
statement.
import random
def quickselect(arr, k, start, stop):
"""Finds kth order statistic in numbers[start:stop])"""
print("hello world!") # <--- here is the print
pivot_ix = random.randrange(start, stop)
pivot_ix = partition(arr, start, stop, pivot_ix)
pivot_order = pivot_ix + 1
if pivot_order == k:
return arr[pivot_ix]
elif pivot_order < k:
return quickselect(arr, k, pivot_ix + 1, stop)
else:
return quickselect(arr, k, start, pivot_ix)
Suppose quickselect(arr, 3, 0, 10)
is called with arr = [5, 2, 8, 3, 1, 6, 9, 7, 4, 0]
.
Part 1)
What is the fewest number of times that ``hello world!'' can possibly be printed? Your answer should be a number, and not in terms of \(n\).
Part 2)
What is the greatest number of times that ``hello world'' can possibly be printed? Your answer should be a number, and not in terms of \(n\).