之前学的折半查找 现在拿出来复习一下
#include <stdio.h>
int searchItem(int a[],int length,int aim){
int mid, high=length-1, low=0;
while (low<=high){
mid=(low+high)/2;
if (a[mid]<aim){
low=low+1;
}
else if(a[mid]>aim) {
high=high-1;
}
else
return mid;
}
return -1;
}
int main(int argc, const char * argv[]) {
int a[]={1,3,4,6,43,44,46,56,65,78};
int loc=searchItem( a, sizeof(a)/sizeof (int) ,65);
printf("loc = %d \n",loc);
return |