#include<stdio.h>
int num[]={0,1,2,3,4,5,6,7,8,9,10};
int binarySearch(int left,int right,int key)
{
while (left<=right)
{
int mid=(left+right)/2;
if (num[mid]>key)
right=mid-1;
else if(num[mid]<key)
left=mid+1;
else
return 1;
}
return 0;
}
int main()
{
for(int i=1;i<=10;i++)
printf("%d ",binarySearch(1,10,i));
return 0;
}
二分查找(左闭右闭区间)
最新推荐文章于 2024-05-08 22:14:44 发布