代码:
#include <stdio.h> #include <stdbool.h> void BinaryInsertSort(int *a, int n) { int i, j, k, low, high, m; for (i = 1; i < n; i++) { low = 0; high = i - 1; while (low <= high) { m = (low + high) / 2; if (a[m] > a[i]) high = m - 1; else low = m + 1; } if (j != i - 1) { int temp = a[i]; for (k = i - 1; k >= high + 1; k--) a[k + 1] = a[k]; a[k + 1] = temp; } } } void printArray(int *a, int n) { for (int i = 0; i < n; i++) { printf("%d ", a[i]); } printf(" "); } int main() { int a[7] = {5, 2, 1, 8, 10, 23, 22}; BinaryInsertSort(a, 7); printArray(a, 7); return 0; }
运行: