The basic idea of binary search algorithm
The precondition of the binary lookup algorithm is an already ordered sequence (in this article to illustrate the convenience of the problem, assuming that the sequence is in ascending order), so that when looking for the element to find, first and the sequence of elements in the middle of the comparison, if greater than this element, in the second half of the current sequence to continue to find, If it is less than this element, the first half of the current sequence continues to be searched until the same element is found, or the range of the searched sequence is empty.
/*binary search * algorithm thought: 1, the array is sorted (from small to Large), 2, each with the middle of the mid-count, if the equivalent can be directly returned, * if larger than mid will continue to look for the big side, otherwise continue to find the small side. Input: Sorted array-ssource[], array size-array_size, lookup value-key return: found to return the corresponding position, otherwise return-1*/intBinsearch (intSsource[],intArray_size,intkey) { intLow =0, high = array_size-1, Mid; while(Low <=High ) {Mid= (low + high)/2;//get the middle position if(Ssource[mid] = =key)returnMid//locate it and return to the appropriate location if(Ssource[mid] >key) high= Mid-1;//if it is larger than key, look for the lower position Else Low= Mid +1;//if it is smaller than key, look for high position } return-1; }
C-Language binary lookup method (pointer and array implementation)
/** Write a function that performs a binary lookup on a sorted integer table. * The input to the function includes pointers to the table headers, the number of elements in the table, and the values to be searched. * The output of the function is a pointer to the element that satisfies the lookup requirement, when the requested value is not found, output a null pointer * implemented in two versions, one with the array small, the second with the pointer * They all use asymmetric boundaries * Copyright (c) 2012 limingauthor:liming2012-06-21referenced C Traps and pitfaills Chinese editionpage 132-137 * * Find the element is x, array the following table is K, start at 0 <= k < n * Next narrow range lo <= k < Hi, * if lo equals hi, we can justify the element "X" is not in the array * */#include<stdio.h>intArray[] = { 0,1,2,3,4,5,6,7};int*BSEARCH_01 (int*t,intNintx);int*BSEARCH_01 (int*t,intNintx) { intLo =0; intHi =N; while(Lo <hi) { //int mid = (hi + lo)/2; intMID = (hi + lo) >>1; if(X <T[mid]) Hi=mid; Else if(X >T[mid]) Lo= Mid +1; Else returnT +mid; } returnNULL;}int*bsearch_02 (int*t,intNintx);int*bsearch_02 (int*t,intNintx) { intLo =0; intHi =N; while(Lo <hi) { //int mid = (hi + lo)/2; intMID = (hi + lo) >>1; int*p = t + mid;//use the pointer variable p to store the value of the t+mid so that it does not need to be recalculated every time if(X < *p) Hi=mid; Else if(X > *p) Lo= Mid +1; Else returnp; } returnNULL;}//further reduce addressing operations/** Suppose we want to reduce address arithmetic still further * by using pointers instead of subscripts throughout th E program. * * */int*BSEARCH_03 (int*t,intNintx);int*BSEARCH_03 (int*t,intNintx) { int*lo =T; int*hi = t +N; while(Lo <hi) { //int mid = (hi + lo)/2; int*mid = Lo + ((Hi-lo) >>1); if(X < *mid) Hi=mid; Else if(X > *mid) Lo= Mid +1; Else returnmid; } returnNULL;}/** The resulting program looks somewhat neater because of the symmetry **/int*bsearch_04 (int*t,intNintx);int*bsearch_04 (int*t,intNintx) { intLo =0; intHi = N-1; while(Lo <=hi) { //int mid = (hi + lo)/2; intMID = (hi + lo) >>1; if(X <T[mid]) Hi= Mid-1; Else if(X >T[mid]) Lo= Mid +1; Else returnT +mid; } returnNULL;}intMainintargcChar**argv) { int* ret =NULL; int*ret2 =NULL; int*ret3 =NULL; int*ret4 =NULL; RET= bsearch_01 (Array,8,3); Ret2= bsearch_02 (Array,8,6); Ret3= bsearch_03 (Array,8,4); Ret4= Bsearch_04 (Array,8,2); printf ("The result is%d\n", *ret); printf ("The result is%d\n", *Ret2); printf ("The result is%d\n", *Ret3); printf ("The result is%d\n", *ret4); printf ("Hello world\n"); return 0;}
Recursive method
intBinsearch (intArray[],intLowintHighintKey/*the value to find*/){ if(low<=High ) { intMid = (Low+high)/2; if(Key = =Array[mid])returnmid; Else if(key<Array[mid])returnBinsearch (array,low,mid-1, key); Else if(key>Array[mid])returnBinsearch (array,mid+1, High,key); } Else return-1;}
Non-recursive method
intBinsearch (intArray[],intSizeofarray,intKey/*the value to find*/){ intlow=0, high=sizeofarray-1; intmid; while(low<=High ) {Mid= (Low+high)/2; if(key==Array[mid])returnmid; if(key<Array[mid]) high=mid-1; if(key>Array[mid]) Low=mid+1; } return-1;}
Algorithm and implementation of sub-search