首页 > 其他分享 >基本功练习_2_27_之二分法查找

基本功练习_2_27_之二分法查找

时间:2023-03-01 10:04:31浏览次数:46  
标签:high 27 return int mid 二分法 low key 基本功

#include<stdio.h>

int search(int *a,int key,int low,int high)

{

int mid;

if(low>high)

return -1;

mid=(low+high)/2;

if(a[mid]==key)return mid;

else if(a[mid]>key)return search(a,key,low,mid-1);

else return search(a,key,mid+1,high);

}

int main(void)

{

int a[]={

1,2,3,4,5,6,7,8,9,12,13,45,67,89,99,101,111,123,134,565,677

};

int i=search(a,99,0,sizeof(a)/sizeof(a[0])-1);

printf("99是数组中第%d个数\n",i+1);

return 0;

}

标签:high,27,return,int,mid,二分法,low,key,基本功
From: https://blog.51cto.com/u_15983838/6092926

相关文章