数组二分查找 求高手解答 哪里错了
/*编译通过也可以运行 就是 查找哪里 好像只能查到中间那个数如果是因为插入一个数使数组长度变化了 我就把 chazao() 中的 high=a 还是不行
还直接把插入那段代码注释掉 还是不行
请教高手 到底哪里错了 要怎么改正*/
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int chazao(int x, int str[], int a)
{
int low, high, mid;
low = 0;
high = a - 1;
while (low <= high)
{
mid = (low + high) / 2;
if (x < str[mid])
{
high = mid - 1;
}else
if (x > str[mid])
{
low = mid + 1;
}
else if (x = str[mid])
{
printf("查到成功过,查找了!str[%d]=%d", mid, x);
break;
}
//printf("\n查找失败");
//return 0;
}
printf("\n查找失败");
return 0;
}
void main()
{
int str[100], a;
int h;
A: printf("输入数组个数:");
scanf("%d", &a);
printf("输入数组:");
for (int i = 0; i < a; i++)
{
scanf("%d", &str[i]);
//printf("% d", str[i]);
}
for (int i = 0; i < a - 1; i++)
{
for ( int j = i+1; j < a; j++)
{
if (str[i] < str[j])
{
h = str[i];
str[i] = str[j];
str[j] = h;
}
}
}
printf("\n排序后\n");
for (int i = 0; i < a; i++)
{
printf("% d",str[i]);
}
printf("\n输入要插入的数:");
int b,c;
int a1, a2;
scanf("%d", &b);
c = str[a];
if (b > c)
{
str[a] = b;
}
else
for (int i = 0; i < a; i++)
{
if (str[i]>b)
{
a1 = str[i];
str[i] = b;
for (int j = i + 1; j < a; j++)
{
a2 = str[j];
str[j] = a1;
a1 = a2;
}
break;
}
}
for (int i = 0; i < a ; i++)
{
for (int j = i + 1; j < a+1; j++)
{
if (str[i] < str[j])
{
h = str[i];
str[i] = str[j];
str[j] = h;
}
}
}
for (int k = 0; k < a + 1; k++)
{
printf("% d", str[k]);
}
printf("\n");
B: printf("输入要查找的数:");
int x;
scanf("%d", &x);
chazao(x, str, a);
goto B;
////goto A;
system("pause");
}