public class BinaryInsertSort {
public void binaryInsertSortTest(){
int[] queue = new int[]{5,3,15,0,1,29,14,4,3,2,0};
print(queue);
binaryInsertSort(queue,0,queue.length-1);
print(queue);
}
public void print(int[] queue){
int length = queue.length;
int index;
for(index=0;index<length;index++){
System.out.print(queue[index]+" ");
}
System.out.println();
}
public void binaryInsertSort(int[] queue,int left,int right){
int temp,i,low,high,middle,k;
for(i=left+1;i<=right;i++){
temp = queue[i];low=left;high=i-1;
while(low<=high){
middle=(low+high)/2;
if(queue[middle]<temp)
low=middle+1;
else high = middle-1;
}
for(k=i-1;k>=low;k--)queue[k+1]=queue[k];
queue[low]=temp;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryInsertSort sort = new BinaryInsertSort();
sort.binaryInsertSortTest();
}
}
转载于:https://my.oschina.net/shadowalker1990/blog/203292