二分法查找

#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH_INSERT_SORT 7

/*
**二分法查找
*/

int binary_search(int a[],int lo,int hi,int key)
{
    int mid=lo+(hi-lo)/2;

    if(key<a[mid]) return binary_search(a,lo,mid-1,key);
    else if(key>a[mid]) return binary_search(a,mid+1,hi,key);
    else return mid;
}
int main()
{
    int b[9]={1,2,3,4,5,6,7,8,9};

    printf("%d",binary_search(b,0,8,3));
    return 0;
}

你可能感兴趣的:(二分法查找)