Binary search algorithm for ordered and unordered arrays

Source: Internet
Author: User

Topic meaning

1, given an ordered array A and keyword key, determine whether a key exists, if present, return the subscript value, does not exist then return-1.

2, given the unordered array A and keyword key, determine whether a key exists, if there is a return 1, does not exist then return 0.

For the 1, 2 problem, we can simply write the O (n) from start to finish scanning algorithm, here is not cumbersome, here we discuss is based on binary search algorithm, so that its time in a progressive sense O (Logn).

For an ordered array, it is "easy" to write out a binary-based function,

So question 2, for unordered arrays, how do you find them? Here we use the principle of dividing the fast sort. The algorithm is roughly as follows:

Function call BinarySearch (A,n,key); A[1..N]

Find x exists in unordered array a[n], if there is a return of 1, there is no return 0

Here we use the classification rules in the quick sort, the general meaning is as follows

A[p the array: R] divided into two words group a[p. Q-1] and A[Q+1..R], making

A[p. Each element in Q-1] is less than or equal to a[q] and, less than or equal to A[Q+1..R]

For Find keywords key,if (key==a[q]) return 1;

if (Key<a[q]) find the word group a[p: Q-1]

if (Key>a[q]) find word group A[Q+1..R]


Code and comments:


<span style= "FONT-SIZE:18PX;" >/** * @xiaoran * * #include <iostream> #include <cstdio> #include <map> #include <cstring># include<string> #include <algorithm> #include <queue> #include <vector> #include <stack> #include <cstdlib> #include <cctype> #include <cmath> #define LL Long longusing namespace std;/** * Binary search * Function call BinarySearch (A,0,n-1,key); * in ordered array A[n] to find whether X exists, if there is a return subscript, there is no return-1 */int binarysearch (int *a,int left,int right,int) {//in ordered array &key] to find whether X exists,        If there is a return subscript, there is no return-1 while (left<=right) {int mid= (left+right)/2;        if (Key==a[mid]) return mid;        else if (Key>a[mid]) left=mid+1;    else right=mid-1; } return-1;} /** * Binary Lookup * Function call BinarySearch (A,n,key); * Find out if x exists in unordered array a[n], if there is a return of 1, there is no return 0 * Here we use the Division rule in the quick sort, the approximate meaning is as follows * The array is a[p. R] divided into two words group a[p. Q-1] and A[Q+1..R], making *a[p. Each element in Q-1] is less than or equal to a[q] and, less than or equal to A[Q+1..R] * for lookup keyword key,if (key==a[q]) return 1; *if (Key<a[q]) find word group a[p. Q-1] *if (key>a[q]) find word group A[Q+1..R] */iNT Binary_init (int *a,int p,int r) {int tmp,i,j;    TMP=A[P]; i=p+1;    J=r;        while (true) {while (a[i]<=tmp) ++i;        while (a[j]>tmp)--j;        if (j<=i) break;            else{swap (a[i],a[j]); ++i;        --j; }} swap (A[p],a[j]);//The keyword is placed in the middle return j;//returns the position of the keyword}int binarysearchplus (int *a,int l,int r,int key) {if (l<r)        {int mid=binary_init (A,L,R);        cout<<a[mid]<< "" <<key<<endl;        if (A[mid]==key) return 1;//yes else if (Key<a[mid]) {//Search left half return binarysearchplus (A,l,mid-1,key);        else{//key>a[mid],//search Right half return binarysearchplus (A,mid+1,r,key);    }} if (L==r) return a[l]==key;//only one element return 0;//no}int main () {int a[9]={0,9,4,3,12,5,34,55,44};    int key;        while (Cin>>key) {cout<<binarysearch (A,1,8,key) <<endl;    Cout<<binarysearchplus (A,1,8,key) <<endl; }return 0;} </span> 


Binary search algorithm for ordered and unordered arrays

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.