(用来做笔记,欢迎交流讨论!)
贴上代码:
package com.fei.example.lib;
public class BinInsertSort {
//折半插入排序
public void binInsertSort(int[] 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];
}
}
}
//测试数据
public static void main(String[] args) {
BinInsertSort a = new BinInsertSort();
int[] array = {0, 49, 38, 65, 97, 76, 13, 27, 49};
int n = array.length - 1;
a.binInsertSort(array, n);
for (int i = 1; i <= n; i++) {
System.out.print(array[i] + " ");
}
}
}