#include<stdio.h>
#include<malloc.h>
void Binary_Insert_Sort(int a[],int length)
{
for(int i=1;i<length;i++)
{
int temp=a[i]; //保存待插入元素
for(int low=0,high=i-1;low<=high;)
{
int mid=(low+high)>>1;
if(temp>a[mid])
low=mid+1; //插入位置在高半区中
else
high=mid-1; //插入位置在低半区中
}
for(int j=i-1;j>=high+1;j--) //high+1为插入位置
a[j+1]=a[j]; //后移元素,留出插入空位
a[high+1]=temp; //将元素插入
}
}
void main()
{
int A[9]={8,8,3,111,6,33,100,9,210};
Binary_Insert_Sort(A,9);
printf("输出排序后:\n");
for (int i = 0; i < 9; i++)
printf("%d ", A[i]); //等价于 *(A+i)
getchar();getchar();
}
插入排序之--折半插入排序
最新推荐文章于 2022-06-20 23:46:46 发布