我是一名职高生 在学c语言这个是2分查找法 看懂了一些 但大部分还是不懂 希望大家帮我解析一下这个程序

#include".h"
#define N 10
main
{
int i,j,number,top,bott,mid,loca,a[N],flag;
char c;
printf("输入15个数\n");
for(i=0;i++;i<15)
scanf("%d",&a[i]);
loca=0;
top=0;
bott=N-1;
if((number<a[0])||(number>a[N-1]))
loca=-1;
while((loca>=0)&&(top<=bott))
{
mid=(bott+top)/2;
if(number==a[mid]
{
loca=mid;
printf("%d位于表中第%个数\n",number,loca+1);
}
else if(nuber<a[mid])
bott=mid-1;
else
top=mid+1;
}
if(loca==0||loca==-1)
printf("%d不在表中。\n",number);
}

#include".h"
#define N 10
main
{
int i,j,number,top,bott,mid,loca,a[N],flag;
char c;
printf("输入15个数\n");
for(i=0;i++;i<15)
scanf("%d",&a[i]);
loca=0;
top=0;
bott=N-1;
if((number<a[0])||(number>a[N-1]))
loca=-1;
while((loca>=0)&&(top<=bott))
{
mid=(bott+top)/2;
if(number==a[mid]
{
loca=mid;
printf("%d位于表中第%个数\n",number,loca+1);
}
else if(nuber<a[mid])
bott=mid-1;
else
top=mid+1;
}
if(loca==0||loca==-1)
printf("%d不在表中。\n",number);
}

你这代码错误太多了啊。
for(i=0;i++;i<15)这里,应该是for(i=0;i<15;i++)
if((number<a[0])||(number>a[N-1]))这里,number没有初始化就用了。
二分法查找的前提是数组已经从小到大排好(或者从大到小排好了,这里说明的时候,默认数组已经按照从小到大排好了),然后比较number与数组中间值的大小,如果number比a[mid]大,那么再跟mid到N中间的数进行比较,直到找到number或者查找结束。
代码如下:

#include <stdio.h>

//数组以升序排列时,二分法查找x
int binSearch(int x, int a[], int n)
{
    int low, high, mid;
    low = 0;
    high = n-1;
    while(low <= high)
    {
        mid = (low + high) / 2;
        if(x < a[mid])
            high = mid - 1;
        else if(x > a[mid])
            low = mid + 1;
        else
            return mid;
    }
    return -1;
}
int main()
{
    int i,number,pos;
    int a[15];
    printf("请输入要查找的数:");
    scanf("%d",&number);
    printf("请输入15个整数(升序):");
    for(i=0;i<10;i++)
        scanf("%d",&a[i]);
    pos = binSearch(number,a,15);
    if(pos < 0)
        printf("未找到%d",number);
    else
        printf("%d位于表中第%d个数",number,pos+1);
    return 0;
}

https://www.cnblogs.com/ponynice/p/12743991.html
https://www.cnblogs.com/mu-ge/p/14543293.html?ivk_sa=1024320u
http://c.biancheng.net/view/3428.html

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632