使用Java实现常见的排序算法
public class Test2_ArraySort {
private int[] getArr(int length) {
int[] re = new int[length];
for (int i = 0; i < re.length; i++) {
re[i] = (int) (Math.random() * 100 + 10);
}
return re;
}
private void printArray(String message, int[] arr) {
System.out.println(message + " : " + Arrays.toString(arr));
}
private void shuffle(int[] re) {
for (int i = 0; i < re.length; i++) {
re[i] = (int) (Math.random() * 100 + 10);
}
}
private void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
private void XZSort(int[] arr, BiPredicate<Integer, Integer> judge) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (judge.test(arr[i], arr[j])) {
swap(arr, i, j);
}
}
}
}
private void MPSort(int[] arr, BiPredicate<Integer, Integer> judge) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (judge.test(arr[j], arr[j + 1])) {
swap(arr, j, j + 1);
}
}
}
}
private void CRSort(int[] arr, BiPredicate<Integer, Integer> judge) {
for (int i = 1; i < arr.length; i++) {
int j = i;
while (j - 1 >= 0 && judge.test(arr[j], arr[j - 1])) {
swap(arr, j, j - 1);
j--;
}
}
}
private int[] GBSort(int[] arr, BiPredicate<Integer, Integer> judge) {
if (arr.length < 2) {
return arr;
} else {
int mid = arr.length / 2;
int[] left = Arrays.copyOfRange(arr, 0, mid);
int[] right = Arrays.copyOfRange(arr, mid, arr.length);
left = GBSort(left, judge);
right = GBSort(right, judge);
int[] re = new int[left.length + right.length];
int index = 0;
int i = 0, j = 0;
while (i < left.length && j < right.length) {
if (judge.test(left[i], right[j])) {
re[index++] = left[i++];
} else {
re[index++] = right[j++];
}
}
while (i < left.length) {
re[index++] = left[i++];
}
while (j < right.length) {
re[index++] = right[j++];
}
return re;
}
}
private void KSort(int[] arr, int start, int end, BiPredicate<Integer, Integer> judge) {
if (end - start >= 1) {
int anchor = (int) (Math.random() * (end - start + 1) + start);
swap(arr, anchor, end);
int i = start;
int j = start;
while (j < end) {
if (judge.test(arr[j], arr[end])) {
swap(arr, j, i);
i++;
}
j++;
}
swap(arr, i, end);
KSort(arr, start, i - 1, judge);
KSort(arr, i + 1, end, judge);
}
}
private void headDown(int[] arr, int start, int end, BiPredicate<Integer, Integer> judge) {
int target = start;
int left = start * 2 + 1;
int right = start * 2 + 2;
if (left < end && judge.test(arr[target], arr[left])) {
target = left;
}
if (right < end && judge.test(arr[target], arr[right])) {
target = right;
}
if (target != start) {
swap(arr, target, start);
headDown(arr, target, end, judge);
}
}
private void headIfy(int[] arr, BiPredicate<Integer, Integer> judge) {
for (int i = (arr.length - 1) / 2; i >= 0; i--) {
headDown(arr, i, arr.length, judge);
}
}
private void headSort(int[] arr, BiPredicate<Integer, Integer> judge) {
headIfy(arr, judge);
for (int i = 0; i < arr.length; i++) {
swap(arr, 0, arr.length - 1 - i);
headDown(arr, 0, arr.length - 1 - i, judge);
}
}
public static void main(String[] args) {
Test2_ArraySort ta = new Test2_ArraySort();
int[] src = ta.getArr(100);
ta.printArray("原始数组", src);
ta.XZSort(src, (n1, n2) -> n1 > n2);
ta.printArray("选择排序,逆序", src);
ta.shuffle(src);
ta.printArray("重新生成", src);
ta.MPSort(src, (n1, n2) -> n1 > n2);
ta.printArray("冒泡排序, 逆序", src);
ta.shuffle(src);
ta.printArray("重新生成", src);
ta.CRSort(src, (n1, n2) -> n1 < n2);
ta.printArray("插入排序, 逆序", src);
ta.shuffle(src);
ta.printArray("重新生成", src);
src = ta.GBSort(src, (n1, n2) -> n1 < n2);
ta.printArray("归并排序, 逆序", src);
ta.shuffle(src);
ta.printArray("重新生成", src);
ta.KSort(src, 0, src.length - 1, (n1, n2) -> n1 < n2);
ta.printArray("快排, 逆序", src);
ta.shuffle(src);
ta.printArray("重新生成", src);
ta.headSort(src, (n1, n2) -> n1 < n2);
ta.printArray("堆排, 逆序", src);
}
}