#include<stdio.h>
#include<stdlib.h>
int main(){
int a[1000];
printf("input the length:");
int length;
scanf("%d",&length);
printf("input some data:\n");
for(int i=1;i<=length;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<length;i++)//从小到大
{
for(int j=i;j<=length;j++){
if(a[i]>a[j]){
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
int low=0;
int high=length;
int mid=(low+high)/2;
printf("input what num would you find?\n");
int temp;
scanf("%d",&temp);
while(low<=high){
if(temp<mid){
high=mid-1;
}
else if(temp>mid){
low=mid+1;
}
else if(temp==mid){
printf("success find!\n");
break;
}
mid=(low+high)/2;
}
if(low>high)printf("can't find the num\n");
}
c语言数据结构---二分查找
最新推荐文章于 2024-11-03 22:17:06 发布