折半插入排序

来源:互联网 发布:优酷网络 编辑:程序博客网 时间:2024/05/21 10:55
import java.util.Arrays;public class BinaryInsertSort {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubint[] a={48,1,2,5,19,3,35,1};sort(a);System.out.println(Arrays.toString(a));}/** * 核心思想:对前面有序数列进行2分查找,找到相应的插入位置 * 然后把相应的插入位置向后移动一位 * @param a */   public static void sort(int[] a){   for(int i=1;i<a.length;i++){   int temp=a[i];   int low=0;   int hight=i-1;   while(low<=hight){   int mid=(low+hight)/2;   if(a[mid]<temp){   low=mid+1;   }else{   hight=mid-1;   }   }   int j;   for(j=i;j>low;j--){   a[j]=a[j-1];   }   a[low]=temp;   }   }}