Exercise 9.1.2
n/2+2(n/2−1)=n/2+n−2=3n/2−2
Exercise 9.2.4
http://clrs.skanev.com/09/01/02.html Prove the lower bound of⌈3n/2⌉−2 comparisons in the worst case to find both the maximum and minimum ofn numbers. (Hint: Consider how many numbers are potentially either the maximum or minimum, and investigate how a comparison affects these counts.)
Each comparison can reduce both the potential minimums and maximums by one. Note that this is now always the case if we make two comparisons to infer that a<b and a<c , we have excluded two elements from the potential minimums (b and c ), but only one from the potential maximums (a ).
We can optimize by splitting the input in pairs and comparing each pair. After n/2 comparisons, we have reduced the potential minimums and potential maximums to n/2 each. Furthermore, those two sets are disjoint so now we have two problems, one minimum and one maximum, each of size n/2 . The total number of comparisons is:
This assumes that n is even. If n is odd we need one additional comparison in order to determine whether the last element is a potential minimum or maximum. Hence the ceiling.
Write an iterative version of RANDOMIZED-SELECT.int randomized_select(int *A, int p, int r, int i) { while (p < r - 1) { int q = randomized_partition(A, p, r); int k = q - p; if (i == k) { return A[q]; } else if (i < k) { r = q; } else { p = q + 1; i = i - k - 1; } } return A[p]; } int partition(int *A, int p, int r) { int x, i, j; x = A[r - 1]; i = p; for (j = p; j < r - 1; j++) { if (A[j] < x) { EXCHANGE(A[i], A[j]); i++; } } EXCHANGE(A[i], A[r - 1]); return i; } int randomized_partition(int *A, int p, int r) { int pivot = rand() % (r - p) + p; EXCHANGE(A[pivot], A[r - 1]); return partition(A, p, r); }
Exercise 9.2.4
Suppose we useRANDOMIZED-SELECT
to select the minimum element of the arrayA=⟨3,2,9,0,7,5,4,8,6,1⟩ . Describe a sequence of partitions that results in a worst-case performance ofRANDOMIZED-SELECT
.
This happens if all the elements get picked up in reverse order that is, the first pivot chosen is 9, the second is 8, the third is 7 and so on.
https://github.com/gzc/CLRS/blob/master/C09-Medians-and-Order-Statistics/9.3.mdExercises 9.3-1
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.
Answer
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
Exercises 9.3-2
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.
Answer
Exercises 9.3-3
Answer
比较直观,我们先利用线性时间去找到中位数,再用这个中位数去做partition.
It is obvious, we first use O(n) time to find median, then use median to do partition.
Exercises 9.3-4
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.
Answer
只用比较来确定的话,就跟这讲的方法是一样的. 我们既然找到了第i小元素,那么比i大和比i小的都已经被我们分类分好了.
We can find ith element, then the number greater than i and the number less than i have been already partitioned.
Exercises 9.3-5
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.
Answer
Find the median, and recursve half of the elements.
Exercises 9.3-6
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.
Answer
- 如果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.
Exercises 9.3-7
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.
Answer
- 计算出中位数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
Exercises 9.3-8
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.
Answer
Divide and conquer
Exercises 9.3-9
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.
Answer
Find the median of y.