程序实现:
#include<stdio.h>
int search(int arr[],int len,int key){
int low = 0, high = len-1, mid;
while (low<=high){
mid = (low+high)/2;
if(key>arr[mid]){
low = mid+1;
}else if(key<arr[mid]){
high = mid-1;
}else{
return mid;
}
}
printf("数组不存在你要查找的数");
return -1;
}
int main(){
int a[] = {2,4,7,10,15,19,23,27,32,35,38,41,45,49,50};
int key;
printf("请输入要查找的数");
scanf("%d",&key);
int loc = search(a, 15, key);
printf("loc = %d\n", loc);