In the algorithm SELECT, the input elements are divided into groups of 5. Will the algorithm work in linear time if they are divided into groups of 7? Argue that SELECT does not run in linear time if groups of 3 are used.
Assuming each group has k elements.
The number that less(or greater) then median of median is at least n/4-k,In the worst case, next call to SELECT will recursive call 3n/4+k elements.
Assuming for all n,T(n) <= cn
So according to 1/k+3/4 <= 1 wo get k >= 4
Analyze SELECT to show that if n ≥ 140, then at least ⌈n/4⌉ elements are greater than the median-of-medians x and at least ⌈n/4⌉ elements are less than x.
Show how quicksort can be made to run in O() time in the worst case.
比较直观,我们先利用线性时间去找到中位数,再用这个中位数去做partition.
It is obvious, we first use O(n) time to find median, then use median to do partition.
Suppose that an algorithm uses only comparisons to find the ith smallest element in a set of n elements. Show that it can also find the i - 1 smaller elements and the n - i larger elements without performing any additional comparisons.
只用比较来确定的话,就跟这讲的方法是一样的. 我们既然找到了第i小元素,那么比i大和比i小的都已经被我们分类分好了.
We can find ith element, then the number greater than i and the number less than i have been already partitioned.
Suppose that you have a "black-box" worst-case linear-time median subroutine. Give a simple, linear-time algorithm that solves the selection problem for an arbitrary order statistic.
code tells everything. Thanks original code here.
Find the median, and recursve half of the elements.
The kth quantiles of an n-element set are the k - 1 order statistics that divide the sorted set into k equal-sized sets (to within 1). Give an O(n lg k)-time algorithm to list the kth quantiles of a set.
code tells everything. Thanks original code here.
-
如果k是偶数,那么取中间一个,再对左右两边进行递归.
-
如果k是奇数,取最接近中间的两个数,再对左右两边进行递归.
-
If k is even, then choose the middle one, recursive call left part and right part.
-
If k is odd,choose two index numbers most close to median,then recursive call the left part and right part.
Describe an O(n)-time algorithm that, given a set S of n distinct numbers and a positive integer k ≤ n, determines the k numbers in S that are closest to the median of S.
- 计算出中位数median
- 将所有数减去median,再取绝对值
- 用SELECT计算出第k小数字y
- 遍历数组,取出所有绝对值小于等于y的
代码的局限性在于只能接收绝对值不含有相同元素的.
- Find the median
- For all the numbers, minus median then get the absolute value
- use SELECT to find the kth smallest number y
- Iterate the array choose all the absolute value that less than y
Let [1 .. n] and Y [1 .. n] be two arrays, each containing n numbers already in sorted order. Give an O(lg n)-time algorithm to find the median of all 2n elements in arrays X and Y.
Divide and conquer
Professor Olay is consulting for an oil company, which is planning a large pipeline running east to west through an oil field of n wells. From each well, a spur pipeline is to be connected directly to the main pipeline along a shortest path (either north or south), as shown in Figure 9.2. Given x- and y-coordinates of the wells, how should the professor pick the optimal location of the main pipeline (the one that minimizes the total length of the spurs)? Show that the optimal location can be determined in linear time.
Figure 9.2: Professor Olay needs to determine the position of the east-west oil pipeline that minimizes the total length of the north-south spurs.
Find the median of y.
Follow @louis1992 on github to help finish this task.