#include <stdio.h>
#define N 8
int main()
{
int a[N+1];
int low,high,i,k,mid,point;
printf("Please enter %d order data:",N);
for(i=0;i<N;i++)
scanf("%d",&a[i]); //按从小到大的顺序输入数据
printf("Please enter the number to be located:");
scanf("%d",&k); //输入要查找的数据
low=0;
high=N-1;
while(low<=high) //二分查找
{
mid=(low+high)/2;
if(a[mid]==k)
{
point=mid; //记录查找值的位置
break;
}
else if(a[mid]>k) high=mid-1;
else low=mid+1;
}
if(low<=high) //如果查找成功则删除数据
{
printf("The index of data is:%d, Now delete it.\n",point); //显示查找值的下标
for(i=point;i<N;i++) //删除数据
a[i]=a[i+1];
for(i=0;i<N-1;i++)
printf("%5d",a[i]);
printf("\n");
}
else //如果查找失败则插入数据
{
printf("The data is not in the array! Now insert.\n ");
i=N-1;
while(i>=0 && a[i]>k) //查找并空出插入数据的位置
{
a[i+1]=a[i];
i=i-1;
}
a[++i]=k; //插入数据
for(i=0;i<N+1;i++)
printf("%4d",a[i]);
printf("\n");
}
return 0;
}
12-01
3195
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-09
5323
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交