C ++ divide and conquer strategy to achieve linear time selection

Problem Description:

Linear order given concentration n elements and an integer k, 1≤k≤n, asked to identify which of the n elements of the elements of a small k, i.e., if these n elements linearly arranged in order according to their time, in the first row k is the number of elements to find the elements.

Notice :( detail compared with the previous essay)

(1) is provided for setting the number of manual input running

(2) cancel the read files directly generates a random number lookup sort

(3) expanding the range of the random number, the size of the array may apply

(4) Time statistics are accurate to the microsecond level

After (5) run a program to enhance stability of the write-once, write data may be used for data analysis charts

Algorithm works:

The elements are divided into n input ⌈n / 5⌉ groups, each consisting of five elements, not only may have a group of five elements. Using any of a sorting algorithm, the elements in each group sorted and removed the median for each group, a total of ⌈n / 5⌉ one. Select recursive call algorithm to find the median ⌈n / 5⌉ elements. If ⌈n / 5⌉ even, to find the median in its two larger one. With this element as the division standard. This sort of recursive find the desired item k.

  . 1 #include <the iostream>
   2 #include <cstdio>
   . 3 #include <the cstdlib>
   . 4 #include <CString>
   . 5 #include <the fstream>
   . 6 #include <algorithm>
   . 7 #include <WINDOWS.H>
   . 8 #include <the ctime>
   . 9  the using  namespace STD;
 10 the LARGE_INTEGER nFreq; // the LARGE_INTEGER in the LONGLONG system 64, are both high and low 32-bit 32-bit LONG system, by windows.h defined as pre-compiled macro 
. 11 the LARGE_INTEGER nBeginTime; / / counter value recorded at the start 
12 is the LARGE_INTEGER nEndTime; // record the time of stopping the counter value
 13 is  
14  //A quick drain 
15  int the Partition ( int the nums [], int P, int R & lt, int x)
 16  {
 . 17      IF (P> R & lt) return - . 1 ;
 18 is      // position x and the reference to find the first exchange 
19      for ( int I = P; I <= R & lt; I ++ )
 20 is      {
 21 is          IF (the nums [I] == X)
 22 is          {
 23 is              the swap (X, the nums [P]);
 24              BREAK ;
 25          }
 26 is      }
 27     int left=p,right=r;
 28     while(left<right)
 29     {
 30         while(left<right && nums[right]>=x) right--;
 31         nums[left]=nums[right];
 32         while(left<right && nums[left]<x) left++;
 33         nums[right]=nums[left];
 34     }
 35     nums[left]=x;
 36     return left;
 37 }
 38 
 39 //快速排序
 40 void QuickSort(int nums[],int low,int high)
 41 {
 42     if(low>high) return;
 43     int key=nums[low];
 44     int left=low,right=high;
 45     while(left<right)
 46     {
 47         while(left<right && nums[right]>=key) right--;
 48         nums[left]=nums[right];
 49         while(left<right && nums[left]<key) left++;
 50         nums[right]=nums[left];
 51     }
 52     nums[left]=key;
 53     QuickSort(nums,low,left-1);
 54     QuickSort(nums,left+1,high);
 55 }
 56 
 57 int Select(int nums[],int p,int r,int k)
 58 {
 59     if(r-p<75)
 60     {
 61         QuickSort(nums,p,r);
 62         return nums[p+k-1];
 63     }
 64     //Every 5 as a group, to find the median of each group, and stores the first (rp-4) / 5 positions in 
65      for ( int I = 0 ; I <= (rp- . 4 ) / . 5 ; I ++ )
 66      {
 67          the QuickSort (the nums, P + . 5 * I, P + . 5 * I + . 4 );
 68          the swap (the nums [P + I], the nums [P + . 5 * I + 2 ]);
 69      }
 70      // Get all median median 
71 is      int X = the Select (the nums, P, P + (rp- . 4 ) / . 5 , (rp- . 4 ) / 10 );
 72      //With x as a reference to do a quick drain 
73 is      int I = the Partition (the nums, P, R & lt, x);
 74      int J = I-P + . 1 ;
 75      // Analyzing k belongs to that part of the 
76      IF (k <= J)
 77          return SELECT (the nums, P, I, K);
 78      the else 
79          return SELECT (the nums, I + . 1 , R & lt, - K- J);
 80  }
 81  int main () {
 82      ofstream FOUT;
 83      Double cost;
 84      int I = 0 , m = 0 , n-= 0 , = Key0 ;
 85      COUT << " Please Enter The Number of Times you want to RUN The Program: " ;         // input program running times 
86      CIN >> m;
 87      int DATA_AMOUNT [m];
 88      Double Runtime [m];
 89      srand ((unsigned int ) Time (NULL));       // set random seed 
90      for (I = 0 ; I <m; I ++ )
 91 is      {
 92          n-= 10000 + and RAND_MAX * (RAND ()% 300 ) + RAND () ;            //RAND_MAX = 32767, randomly generated data amount 
93          DATA_AMOUNT [I] n-=;                                // defines the data size of ~ 9,872,867 10000 
94          COUT << " ☆ of The " << I + . 1 << " TH Test the Data AMOUNT IS: " << n- << endl;
 95          int J = 0 ;
 96          int * A = ( int *) the malloc (n-* the sizeof ( int ));
 97          for (J = 0 ; J <n-; J ++ ) {
 98              A [J] = and RAND_MAX * (RAND ()% 400 ) + RAND ();          // randomly generates a random number of 0 ~ 13139567 
99          }
 100          Key = RAND ()% 10 000 + 1 ;    // randomly generated sequence of 1 to 10000 to be selected as 
101          QueryPerformanceFrequency (& nFreq); // Get the system clock 
102          the QueryPerformanceCounter ( nBeginTime &); // Get start time count value 
103          int T = the Select (A, 0 , N- . 1 , Key);
 104          the QueryPerformanceCounter (& nEndTime); // Get the count value of the stop time 
105          cost = ( Double ) (nEndTime.QuadPart - nBeginTime.QuadPart) / ( Double)nFreq.QuadPart;
106         runtime[i]=cost;
107         cout<<"The "<<key<<"th number is:"<<t<<endl;
108         cout<<"The running time is:"<<cost<<" s"<<endl;
109         free(a);
110     }
111     fout.open("data.txt");
112     if(!fout){
113         cerr<<"Can not open file 'data.txt' "<<endl;
114         return -1;
115     }
116     for(i=0;i<m;i++){
117            fout<<data_amount[i]<<","<<runtime[i]<<endl;
118     }
119     fout.close();
120     cout<<"Success!"<<endl;
121     return 0;
122 }

Programming ideas:

Assuming that the input data size of n, the order of data you're looking for is k.

(1) determining the length of the array, if less than 75 directly fast sort algorithm or later will be.

(2) the elements are divided into n input ⌈n / 5⌉ groups, each group of five elements. By flash sorting elements in each group sorted, and the median is determined in each group, were ⌈n / 5⌉ one.

(3) Select the recursive call algorithm to find the median of this ⌈n / 5⌉ elements. If ⌈n / 5⌉ even, to find the median in its two larger one. With this element as the division standard.

(4) In this recursive calls to sort, search eventually get Item k looking for.

Time Complexity Analysis:

Analysis calculated for time complexity of the algorithm Select, provided n = r-p + 1, i.e., n is the length of the input array. Recursive call algorithm is only executed if n≥75. Thus, when n <75 Select algorithm is to calculate the time used no more than a constant C . 1 . After finding the median of the median x, x is an algorithm to divide Select Partition call reference array a [p: r] be divided, which requires O (n-) time. Select the loop algorithm for performing common row n / 5 times, each time requires O (. 1) time. Thus, for loop total of O (n-) time.

Provided Select n call algorithm of the array elements need T (n-) time, then find the median by the median x is at most T time (n / 5) in. Has been demonstrated, the resulting divided sub-array 2 respectively at most 3n / 4 elements according to the algorithm selected reference x. So, regardless of which sub-array call, Select all up with the T time (3n / 4) of.

In short, can be obtained on T (n-) recursive formula

 

 

 

Solutions of this type can be obtained recursively T (n-) = O (n-) .

After 5000 experiments and statistical data of different sizes run time obtain the following algorithm efficiency analysis chart :

 

Guess you like

Origin www.cnblogs.com/Jesse-Cavendish/p/11693617.html