折半插入排序
public class BinaryInsertionSort {public static void binaryInsertSort(int a[], int left, int right) {for (int i = left + 1; i <= right; i++) {int temp = a[i];int low = left;int high = i - 1;while (low <= high) {int mid = (low + high) / 2;if (temp < a[mid])high = mid - 1;elselow = mid + 1;}for (int j = i - 1; j >= low; j--){a[j + 1] = a[j];}a[low] = temp;}}public static void main(String[] args) {int[] a = { 10, 32, 1, 9, 5, 7, 12, 0, 4, 3 };System.out.print("排序前: ");for (int i = 0; i < a.length; i++)System.out.printf("%3s ", a[i]);System.out.println("");// 进行排序binaryInsertSort( a, 0, a.length-1 );;// 排序后结果System.out.print("排序后: ");for (int i = 0; i < a.length; i++)System.out.printf("%3s ", a[i]);System.out.println("");}}
?
? ?