#include<stdio.h>
int main(){
int a[10] = {50,65,75,78,80,84,85,89,90,95};
int left=0,right=9;
int mid,n=0;
while (left<=right)
{
mid=(left+right)/2;
n++;
if (a[mid]>92)
{
right = mid-1;
}
else if (a[mid]<92)
{
left = mid+1;
}
else
{
printf("找了%d次\n", n);
break;
}
}
if (left > right)
{
printf("找不到\n");
printf("找了%d次\n",n);
}
return 0;
}
在已经排序的基础上实现二分法找数据
最新推荐文章于 2024-08-12 16:30:33 发布