#include <stdio.h>
#define n 10
int binary_s(int b[],int i,int target)
{
int left=0;
int right=i-1;
int mid;
while(left<=right)
{
mid=(left+right)/2;
if(b[mid]>target)
{
right=mid-1;
}
else
{
left=mid+1;
}
}
return mid;
}
int main()
{
int a_size=7;
int right;
int temp=0;
int a[n]={7,6,5,4,3,2,1};
int x;
printf("折半插入排序:\n");
printf("原序列:");
for(int i=0;i<a_size;i++)
{
printf("%d ",a[i]);//输出
if(i==a_size-1)
{
printf("\n");
}
}
for(right=1;right<a_size;right++)
{
x=a[right];
temp=binary_s(a,right,a[right]);//不能把a[]传过去,会报错.error: expected primary-expression before ']' token|
for(int k=right-1;k>=temp;k--)
{
a[k+1]=a[k];
}
a[temp]=x;
}
printf("排序后序列:");
for(int i=0;i<a_size;i++)
{
printf("%d ",a[i]);//输出
}
return 0;
}
运行结果: