public class BinaryInsertSort {
public static void main(String[] args) {
int[] arr ={1,0,2,0,3,0,4,0,5,0,6,0,7,0,8,0,9,0};
int temp, left, right, middle;
for (int out = 1; out < arr.length; out++) {
//将当前待插入记录保存在临时变量中
temp = arr[out];
left = 0;
right = out - 1;
//寻找合适的位置
while (left <= right) {
// 找出中间值
middle = (left + right) / 2;
//判断中位数是否大于temp推断出应该插入哪边
if (arr[middle] > temp) {
// 插入点在左半区
right = middle - 1;
} else {
// 插入点在右半区
left = middle + 1;
}
}
//将比temp大的数向后移动
for (int in = out - 1; in >= left; in--) {
arr[in + 1] = arr[in];
}
//将待插入记录回填到正确位置
arr[left] = temp;
}
for(int i : arr) {
System.out.print(i + " ");
}
}
}
结果:0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9