代码
#include<iostream>
#define N 9
#define ElemType int
/*折半插入排序*/
void BinarySort(ElemType A[], int n) {
int i,j,low , high ,mid;
for (i = 2; i <= n; ++i) {
A[0] = A[i];
low = 1; high = i - 1;
while (low<high)
{
mid = (low + high) / 2;
if (A[mid] > A[0])
high = mid - 1;
else
low = mid + 1;
}
for (j= i-1; j >= high+1; --j)
A[j + 1] = A[j];
A[high + 1] = A[0];
}
}
/*输出数组*/
void OutPrint(ElemType A[]) {
int i;
for (i = 1; i < N; i++)
{
printf("%d ", A[i]);
}
}
int main() {
ElemType A[N] = { -1,48, 62, 35, 77, 55,14,35,98 };//A[0]"哨兵"
printf("排序前数组
");
OutPrint(A);
BinarySort(A,N-1);
printf("
排序后数组
");
OutPrint(A);
system("pause");
return 0;
}
运行结果如图: