标签:
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 else 13 low = mid + 1; 14 } 15 // array[i, low]向后移动一位,插入tmp 16 for (int j = i; j > low; j--) 17 array[j] = array[j - 1]; 18 array[low] = (E) tmp; 19 } 20 return array; 21 }
标签:
原文地址:http://www.cnblogs.com/upside-down/p/5858491.html