"title": Given the n elements of a linear sequence set and an integer k,1≤k≤n, it is required to find the element K small in the N element, (the given linear set is unordered)
"Idea": if a dividing datum can be found in the linear time, so that the length of the 2 sub-arrays that are divided by this datum is at least the size of the original array (0<ε<1 is a certain normal number), then the selection task can be completed in the worst case with O (n) time.
For example, if Ε=9/10, the length of the sub-array generated by the algorithm recursive invocation is reduced by at least 1/10. So, in the worst case, the computation time T (n) required by the algorithm satisfies the recursive t (n) ≤t (9N/10) +o (n). Thus, t (n) =o (n) can be used.
"Specific problem solving": here we will all the number (n), divided into a group of 5, a total of [N/5] group (will be less than five of the group ignored); Then use any sort algorithm (because only five numbers are sorted, so any sort method is OK, here I choose bubble sort), The elements in each group are sorted and then taken to the median of each group, the median number of [N/5] is obtained, then the median of the [N/5] median (if the N/5 is an even number, the larger of its 2 median) is used as a baseline, dividing all the numbers into two parts, less than the datum on the left, The right side of the drop is greater than or equal to the datum.
In this case, the baseline X is found to be at least greater than 3 (n-5)/10 elements, because 2 elements in each group are smaller than the median of this group, the median is 1/2*[n/5-1], that is, there is (n-5)/10 less than the reference x in the N/5 medium number. Similarly, the reference x is smaller than 3 (n-5)/10 elements at least. When n≥75, 3 (n-5)/10≥N/4 so the length of 2 sub-arrays divided by this datum is reduced by at least 1/4.
The program code is as follows:
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#define MAX_VALUE 10000
#define RANDOM () rand ()%max_value
#define N 10000
int a[n];
Class Find
{
Public
void Bubble (int first,int end)//bubble sort
{
for (int flag=first;flag<end;flag++)
for (int i=end;i>flag;i--)
if (A[i]<a[i-1])
{int t=a[i];
A[I]=A[I-1];
a[i-1]=t;
}
}
The elements of int partition (int p,int r,int x)///from a[p] to A[r in array A are divided by x, greater than X on the left, and less than X on the right
{
int i,j;
for (i=p,j=r;i<j;i++)
{
if (a[i]>x)
{
while (I<J&&A[J]>X)
j--;
if (i!=j) {
int t=a[i];
A[I]=A[J];
a[j]=t;
j--;
}
}
}
return i-1;
}
int select (int p,int r,int k)//Find Median
{
if (r-p<5) {
Bubble (P,R);
return a[p+k-1];
}
for (int i=0;i< (r-p-4)/5;i++)
{
int s=p+5*i,t=s+4;
Bubble (s,t);
int Temp=a[p+i];
A[P+I]=A[S+2];
A[s+2]=temp;
}
int X=select (p,p+ (r-p-4)/5, (R-P+6)/10);
I=partition (P,R,X);
int j=i-p+1;
if (K<=J)
Return select (P,i,k);
Else
Return select (I+1,R,K-J);
}
};
void Main ()
{
clock_t Start,end;
double elapsed;
Srand ((int) time (NULL));
for (int k=0;k<n;k++)
{
A[k]=random ();
cout<<a[k]<< "\ t";
}
cout<<endl;
Start=clock ();
Find F;
int n=5000;
cout<< "The No." <<n<< "is:" <<f.select (0,n-1,n) <<endl;
End=clock ();
Elapsed= (Double) (End-start));///clocks_per_sec;
cout<< "Time:" <<elapsed<<endl;
This topic is the key to find a baseline, thus improving the search efficiency, time complexity of O (n);
|