- #include<stdio.h>
- //定义函数
- int zheBan(int arr[],int len,int key){
- //定义最小值,最大值,中间值
- int low,high,mid;
- low=0;
- high=len-1;
- //循环
- while(low<=high){
- //计算mid的值
- mid=(low+high)/2;
- if(key>arr[mid]){
- low=mid+1;
- }
- else if(key<arr[mid]){
- high=mid-1;
- }
- else {
- return mid;
- }
-
- }
- printf("这游戏没法玩儿了\n");
- return -1;
- }
- int main(){
- //定义一个有序的一维数组
-
- int a[5]={1,3,5,6,8};
- //调用函数折半查找
- int loc=zheBan(a,5,66);
- printf("loc=%d\n",loc);
- return 0;
- }
复制代码 |
|