先将算法程序发布
package com.sufa.test;
public class BinaryInsertSort {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = { 46, 58, 15, 45, 90, 18, 10, 62 };
binaryInsertSort(a);
for(int i=0;i<a.length;i++) {
System.out.println(a[i]);
}
}
/*
* 折半排序算法
* */
public static void binaryInsertSort(int[] data){
int length = data.length;
//从第二个数做算法
for(int i=1;i<length;i++) {
//1、记下将要插入的值
int tmp = data[i];
int low = 0;
int high = i-1;
while(low <= high){
int mid = (low + high) / 2;
if(tmp >= data[mid])
{
//如果要插入的值比中间值大,则在后半部查找
low= mid + 1;
}else {
//如果要插入的值比中间值小,则在前半部查找
high = mid - 1;
}
}
//将比插入值要大的数据往后面移动
for(int j=i; j>low; j--){
data[j] = data[j-1];
}
data[low] = tmp;
}
}
}