Chapter 9 median and sequence statistics
9.2 select with expected Linear Time
In the previous section, we found the minimum value, the minimum value, and the minimum value. Selecting Small (large) elements in the array at Will looks more complicated than simply selecting the minimum value. But surprisingly, the two problems have the same running time, both of which are O (n ). In the introduction to algorithms, we introduce a distributed algorithm used to solve the selection problem, namely the randomized-select algorithm. This algorithm is based on the segmentation algorithm of the quick sorting algorithm. Just like in quick sorting, the idea of this algorithm is to recursively divide the input array. However, unlike quick sorting, quick sorting will recursively process both sides of the Division, while Randomized-select will only process one side of the division. This difference is reflected in algorithm analysis: the expected running time of fast sorting is O (nlgn ). The expected running time of randomized-select is O (n ). The following is the randomized-select algorithm:
Randomized-select (A, P, R, I) p109
1 if p = r2 then return A[p]3 q ← RANDOMIZED-PARTITION(A, p, r)4 k ← q - p + 15 if i = k6 then return A[q]7 elseif i < k8 then return RANDOMIZED-SELECT(A, p, q - 1, i)9 else return RANDOMIZED-SELECT(A, q + 1, r, i - k)
Randomized-select utilizes the randomized-partition program. Therefore, this algorithm is also a random algorithm because its behavior is partly determined by the output of the random number generator. This algorithm returns the small I element in a [P. R.
After Randomized-partition in the third row of the algorithm is executed, array a [p .. r] is divided into two sub-arrays A [p .. q-1] And a [q + 1 .. r], so that a [p .. each element in the Q-1] is less than or equal to a [Q], a [q + 1 .. each element in R] is greater than a [Q].
The fourth row calculates the k Number of elements in a [P. R], that is, the number of elements in the low partition plus a primary element. Then, the fifth line checks whether a [Q] is a small I element. If yes, a [Q] is returned. Otherwise, the algorithm needs to determine which of the I-small elements falls into the two sub-arrays A [P. q-1] And a [q + 1. R. If I <K, it is in the low area, for example, I> K, it is in the high area. Then select recursively.
The worst running time of an algorithm is O (n2), even if the minimum element is to be selected.
In the beauty of programming, 2.3 find and post the "Water King" and you can use the sequence statistics to answer the question.
C ++ code
1 #include <iostream> 2 #include <ctime> 3 #include <cstdlib> 4 5 using namespace std; 6 7 void swap(int* x, int* y) 8 { 9 int temp;10 temp = *x;11 *x = *y;12 *y = temp;13 }14 15 inline int random(int x, int y)16 {17 srand((unsigned)time(0));18 int ran_num = rand() % (y - x) + x;19 return ran_num;20 }21 22 int partition(int* arr, int p, int r)23 {24 int x = arr[r];25 int i = p - 1;26 for(int j = p; j < r; j++)27 {28 if (arr[j] <= x)29 {30 i++;31 swap(arr[i], arr[j]);32 }33 }34 swap(arr[i + 1], arr[r]);35 return ++i;36 }37 38 int randomizedpartition(int* arr, int p, int r)39 {40 int i = random(p, r);41 swap(arr[r], arr[i]);42 return partition(arr, p, r);43 }44 45 int randomizedSelect(int* arr, int p, int r, int i)46 {47 if(p == r)48 {49 return arr[p];50 }51 int q = randomizedpartition(arr, p, r);52 int k = q - p + 1;53 if(i == k)54 {55 return arr[q];56 }57 else if(i < k)58 {59 return randomizedSelect(arr, p, q - 1, i);60 }61 else62 return randomizedSelect(arr, q + 1, r, i - k);63 }64 65 int main()66 {67 int arr[] = {1, 3, 5, 23, 64, 7, 23, 6, 34, 98, 100, 9};68 int i = randomizedSelect(arr, 0, 11, 4);69 cout << i << endl;70 return 0;71 }