public static boolean less(Comparable v, Comparable t) {
return v.compareTo(t) < 0;
}
public static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
public static void show(Comparable[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
public static boolean isSorted(Comparable[] a) {
int N = a.length;
for (int i = 1; i < N; i++)
if (less(a[i], a[i - 1])) return false;
return true;
}//工具类</span>
private static void BubbleSort(Comparable[] arr) {
int N = arr.length;
for (int i = 0; i < N - 1; i++) {
for (int j = 0; j < N - 1 - i; j++)
if (SortUtil.less(arr[j + 1], arr[j])) SortUtil.exch(arr, j + 1, j);
}
}//冒泡排序
private static void InsertSort(Comparable[] arr) {
int N = arr.length;
for (int i = 1; i < N; i++) {
for (int j = i; j > 0 && SortUtil.less(arr[j], arr[j - 1]); j--)
SortUtil.exch(arr, j, j - 1);
}
}//插入排序
private static void MergeSort(int lo, int hi) {
if (hi <= lo) return;
int mid = (hi + lo) / 2;
MergeSort(lo, mid);
MergeSort(mid + 1, hi);
Merge(lo, mid, hi);
}
private static void Merge(int lo, int mid, int hi) {
int i = lo, j = mid + 1;
for (int k = lo; k <= hi; k++)
aux[k] = arr[k];
for (int k = lo; k <= hi; k++) {
if (i > mid) arr[k] = aux[j++];
else if (j > hi) arr[k] = aux[i++];
else if (SortUtil.less(arr[j], arr[i])) arr[k] = aux[j++];
else arr[k] = arr[i++];
}
}//归并排序
private static void SelectSort(Comparable[] arr) {
int N = arr.length;
for (int i = 0; i < N - 1; i++) {
int min = i;
for (int j = i + 1; j < N; j++)
if (SortUtil.less(arr[j], arr[min])) min = j;
SortUtil.exch(arr, i, min);
}
}//选择排序
private static void Sort(Comparable[] arr) {
int N = arr.length;
int h = 1;
while (h < N / 3)
h = 3 * h + 1;
while (h >= 1) {
for (int i = h; i < N; i++) {
for (int j = i; j >= h && SortUtil.less(arr[j], arr[j - h]); j -= h) {
SortUtil.exch(arr, j, j - h);
}
}
h = h / 3;
}
}//希尔排序