标题: 折半查找法 [打印本页] 作者: 403295277 时间: 2015-8-28 21:34 标题: 折半查找法 //
// main.c
// zhebanchazhao
//
// Created by on 15/8/8.
// Copyright (c) 2015年 . All rights reserved.
//
#include <stdio.h>
int numSearch(int arr[],int len,int key){
int low,high,mid;
low=0;
high=len-1;
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;
}
}
return -1;
}
int main(int argc, const char * argv[]) {
int a[]={1,2,3,4,5,6,7,8,9,12};
printf("111111111");
for (int i=0; i<10; i++) {
printf("%d\t",a[i]);
}
int num=numSearch(a,10,9);
printf("%d",num);
return 0;
}