首页 > 代码库 > BinaryInsertSort

BinaryInsertSort

code:

 1 @SuppressWarnings({ "unchecked", "rawtypes" }) 2 public static <E> E[] binaryInsertSort(E[] array) { 3     for (int i = 1; i < array.length; i++) { 4         Comparable tmp = (Comparable) array[i]; 5         /* 二分法找出应该插入tmp的位置 */ 6         int low = 0; 7         int high = i - 1; 8         while (low <= high) { 9             int mid = (low + high) >>> 1;10             if (tmp.compareTo(array[mid]) < 0)11                 high = mid - 1;12             else13                 low = mid + 1;14         }15         // array[i, low]向后移动一位,插入tmp16         for (int j = i; j > low; j--)17             array[j] = array[j - 1];18         array[low] = (E) tmp;19     }20     return array;21 }

 

BinaryInsertSort