1.9 折半查找

#include<stdio.h>
#define N 10 
int main()
{
        int i,a[N]={-3,4,7,9,13,45,67,89,100,180},low=0,high=N-1,mid,k=-1,m;
    printf("a数组种的数据如下:\n");
    for(i=0;i<=N;i++)
    printf("%d",a[i]);
    printf("\n");
    printf("Enter m: ");
    scanf("%d",&m);
    while(low<=high)
    {
        mid=(low+high)/2;
        if(m<a[mid])
            high=mid-1;
        else
        if(m>a[mid])
        low=mid+1;
        else{
        k=mid;
        break;
    }
     if(k>=0)
     printf("m=%d,index=%d\n",m,k);
     else
     printf("Not be found!\n");
}

 

posted @ 2023-04-25 19:28  痛苦代码源  阅读(10)  评论(0编辑  收藏  举报