二分查找(递归与非递归)

#include <stdlib.h>
#include <stdio.h>
//二分查找非递归

int Binary_Search(int list[],int key,int length){
    int low=0,high=length-1;
    while (low<=high){
        int mid=(high+low)/2;
        if (list[mid]==key)
            return ++mid;
        else if (list[mid]>key)
            high=mid-1;
        else if (list[mid]<key)
            low=mid+1;
    }
    return -1;
}
int main(){
    int list[10]={1,3,5,6,7,8,9,10,11,19};
    int number=Binary_Search(list,10,10);
    printf("%d",number);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/szj666/p/13405912.html