Introduction to algorithms: 9.3-7 O (n) Time to calculate the number of K nearest to the median

Source: Internet
Author: User

Question:

An O (n) time algorithm is provided. After a set of n different numbers s and a positive integer k <= n, it can determine the number of k s closest to the number of digits in S.

Thoughts:

Step 1: obtain the value O (n) of the median of the array)

Step 2: Calculate the absolute value of the difference between each number and the median in the array and store it in another array B. O (N)

Step 3: Find the number ret o (n) in the k-th small of array B)

Step 4: Calculate the absolute value of the difference between array S and RET smaller than the number of RET and output O (N)

In step 4, we can also use the division method to find the absolute value of the difference between the array S and RET smaller than the number of ret.

Code:

# Include <iostream> using namespace STD; int length_a; void print (int * A) {int I; for (I = 1; I <= length_a; I ++) cout <A [I] <''; cout <Endl ;} ******************* ******************************* // It has already appeared many times, int partition (int * a, int P, int R) {int x = A [R], I = p-1, J; For (j = P; j <R; j ++) {if (a [J] <= x) {I ++; swap (A [I], a [J]) ;}} swap (A [I + 1], a [R]); return I + 1;} int select (int * a, int P, int R, int I ); // insert and sort each group from start to end, and return the value. // The insertion sorting is very simple. Int insert (int * a, int start, int end, int K) {int I, j; for (I = 2; I <= end; I ++) {int T = A [I]; for (j = I; j> = start; j --) {If (j = Start) A [J] = T; else if (a [J-1]> T) A [J] = A [J-1]; else {A [J] = T; break; }}return A [start + k-1];} // according to the algorithm in this paper, find the median int find (int * a, int P, int R) {int I, j = 0; int start, end, Len = r-p + 1; int * B = new int [Len/5 + 1]; // a group of five elements with the length from start to end. insert and sort each group, returns the median value for (I = 1; I <= Len; I ++) {if (I % 5 = 1) Start = I + p-1; if (I % 5 = 0 | I = Len) {J ++; end = I + p-1; // sort the insertion of each group from start to end, returns the value. If it is the last group, the number of elements in the group may be less than 5int ret = insert (a, start, end, (end-Start)/2 + 1 ); // pick out the values in each group to form a new array B [J] = RET ;}// recursively call select () int ret = select (B, 1, J, (J + 1)/2); // Delete [] B; return ret ;} // F-based division int partition2 (int * a, int P, int R, int f) {int I; // locate the position of F and make it exchange with a [R] for (I = P; I <r; I ++) {if (a [I] = f) {swap (A [I], a [R]); break ;}} return partition (A, P, R) ;}// search for Array a [p .. the largest I element in R]. I is counted from 1, not from P. Int select (int * a, int P, int R, int I) {// If the array contains only one element, if (P = r) return a [p]; // based on the algorithm in the text, find the median int F = find (A, P, R); // use this median as the primary element, and return the median value in the entire array a [1 .. len] position // because the principal component is an element in the array, the Division is like this. A [p .. q-1] <= F <A [q + 1 .. r] int q = partition2 (A, P, R, f); // convert the value to the value in array a [p .. r] In the position int K = Q-p + 1; // compare with the searched element if (I = k) return a [Q]; else if (I <k) return select (A, P, Q-1, I); else // if the principal element is an element in the array, the latter half should write return select (, q + 1, R, I-k); // if the principal element is not an element in the array, change the latter half to select (A, Q, R, i-k + 1)} int main () {int K, I; while (CIN> length_a> K) {If (k> length_a) {cout <"error: k> length_a" <Endl; continue;} // generate random data int * A = new int [length_a + 1]; for (I = 1; I <= length_a; I ++) A [I] = rand () % 100; print (); // calculate the median int mid = select (A, 1, length_a, (length_a + 1)/2 ); // calculate the distance between each number and the median int * B = new int [length_a + 1]; for (I = 1; I <= length_a; I ++) B [I] = ABS (A [I]-mid); // select a number smaller than K int ret = select (B, 1, length_a, k ); // obtain the number of rows whose distance from the median is less than RET and output for (I = 1; I <= length_a; I ++) if (ABS (A [I]-mid) <= RET) cout <A [I] <''; cout <Endl; Delete [] ;} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.