Three, linear search algorithm, BFPRT algorithm

Source: Internet
Author: User


| report | Font Subscription

http://ds.fzu.edu.cn/fine/resources/FlashContent.asp?id=82

This is a good video tutorial: A look to understand:

Then let's talk about algorithms:

Termination condition: When n=1, it is the I small element that is returned.

Algorithm steps:

Step1: divides n elements into groups of N/5 (upper bound), each of 5 groups.

Step2: Take out the median of each group, any sort method, such as insert sort.

Step3: Recursive invocation of the selection algorithm to find the median of all the median in the previous step, set to X, the case of an even number of median is set to select the middle of the small one.

STEP4: Use X to divide the array, set the number of less than equals X is K, the number of greater than X is n-k.

STEP5: If i==k, return x, if i<k, in the element less than x recursively find the small element I, if i>k, in the element greater than x recursively find the I-k small element.

Simple analysis: When x is selected in the third step, the element greater than X has at least 3n/10-6, and in the worst case, the number of recursive elements in the fifth step is 7N/10 + 6. The available recursive expressions are as follows:
T (n) <= T (N/5) + t (7N/10 + 6) + O (n)

Principle and quick selection options like

Similar to the segmentation algorithm in the fast line:

The position of the pivot point in the array is returned after each partition s, and then the size of S and K is compared.
If large, then again recursively divided array[s. N],
Small words, recursive array[left...s-1]//s as the intermediate pivot point element.
Otherwise, return Array[s], which is the value returned in partition. is to find this s.

After finding the S value that meets the requirements, traverse the element on the side of the output that is smaller than S.


"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);

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.