C语言谁帮我写个折半插入排序函数

来源:百度知道 编辑:UC知道 时间:2024/09/23 17:22:07
书上的代码看不怎么懂
能不能给个代码
在主函数里面写的
直接可以运行的
有注释更好···
希望有注释啊···

好的我我再加分

#include "stdio.h"
int a[20],b[20];
main()
{ int n,i,low,high,min,t,q,count=0;
scanf ("%d",&n);
for (i=1;i<=n;i++)
scanf ("%d",&a[i]);
b[1]=a[1];
for (i=2;i<=n;i++)
{ t=a[i];
for(low=1,high=i-1;low<=high;)/*循环结束条件l>h*/
{ min=(low+high)/2;
if (t<b[min]) {count++; high=min-1;} /*当t大于中间值*/
else { count++; low=min+1; min++; } /*当t小于中间值*/
}
for(q=i-1;q>=min;q--)
b[q+1]=b[q];
b[min]=t;
}
for(i=1;i<=n;i++)
{ if(b[i]==b[i+1]&&i!=n) continue;
else if(b[i]==b[i+1]&&i==n) printf("%d\n",b[i]);
else if(i==n) printf("%d\n",b[i]);
else printf("%d ",b[i]);
}
printf("%d\n",count);
}