package com.zl; import java.io.IOException; import java.util.Arrays; public class Test { public static void main(String[] args) throws IOException { int arr[] = {3, 4, 2, 1, 6, 5, 7, 8, 0, 10, 9}; System.out.println("选择排序: " + Arrays.toString(sortBySelect(arr))); System.out.println("插入排序: " + Arrays.toString(sortByInsert(arr))); System.out.println("希尔排序: " + Arrays.toString(sortbyXiEr(arr))); System.out.println("快速排序: " + Arrays.toString(sortByQuick(arr, 0, arr.length - 1))); System.out.println("自顶向下的归并排序: " + Arrays.toString(mergeSortForTop(arr, 0, arr.length - 1))); System.out.println("自底向上的归并排序: " + Arrays.toString(mergeSortForBottom(arr, 0, arr.length - 1))); } // 快速排序 public static int[] sortByQuick(int[] arr, int start, int end) { if (start == end) { return arr; } int boundaryLocal = binarySeparateArr(arr, start, end); // 处理左边数组 sortByQuick(arr, start, Math.max(boundaryLocal - 1, start)); // 处理右边数组 sortByQuick(arr, Math.min(boundaryLocal + 1, end), end); return arr; } // 自底向上的归并排序 public static int[] mergeSortForBottom(int[] arr, int start, int end) { for (int i = 1; i < arr.length; i += i) { for (int j = 0; j < arr.length - i; j += 2 * i) { mergeArrWithSort(arr, j, j + i - 1, Math.min(arr.length - 1, j + 2 * i - 1)); } } return arr; } // 自顶向下的归并排序 public static int[] mergeSortForTop(int[] arr, int start, int end) { if (start == end) { return arr; } int mid = (start + end) / 2; // 左边排序 mergeSortForTop(arr, start, mid); // 右边排序 mergeSortForTop(arr, mid + 1, end); // 合并 return mergeArrWithSort(arr, start, mid, end); } // 希尔排序 public static int[] sortbyXiEr(int arr[]) { int h = 1; while (h < arr.length / 3) h = 3 * h + 1; while (h >= 1) { for (int i = h; i < arr.length; i++) { for (int j = i; j >= h; j = j - h) { if (checkLeftSmall(arr[j], arr[j - h])) { exchange(arr, j, j - h); } } } h = h / 3; } return arr; } // 插入排序 public static int[] sortByInsert(int arr[]) { for (int i = 1; i < arr.length; i++) { for (int j = i - 1; j >= 0; j--) { if (!checkLeftSmall(arr[j], arr[j + 1])) { exchange(arr, j, j + 1); } } } return arr; } // 选择排序 public static int[] sortBySelect(int arr[]) { for (int i = 0; i < arr.length - 1; i++) { int min = i; for (int j = i + 1; j < arr.length; j++) { if (!checkLeftSmall(arr[min], arr[j])) { min = j; } } if (min != i) { exchange(arr, min, i); } } return arr; } // 两个数组 归并 public static int[] mergeArrWithSort(int[] arr, int left, int mid, int right) { int temp[] = new int[arr.length]; System.arraycopy(arr, 0, temp, 0, arr.length); int le = left; int ri = mid + 1; for (int i = left; i <= right; i++) { if (le == mid + 1) { arr[i] = temp[ri++]; } else if (ri == right + 1) { arr[i] = temp[le++]; } else if (checkLeftSmall(temp[le], temp[ri])) { arr[i] = temp[le++]; } else { arr[i] = temp[ri++]; } } return arr; } // 二分数组 public static int binarySeparateArr(int arr[], int start, int end) { int boundary = arr[start]; int left = start + 1; int right = end; while (true) { while (left < right && arr[left] <= boundary) { left++; } while (right > left && arr[right] > boundary) { right--; } if (left == right) { break; } else { exchange(arr, left, right); } } exchange(arr, start, left - 1); return left - 1; } // 数据交换 public static void exchange(int arr[], int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } // 验证大小 public static boolean checkLeftSmall(int left, int right) { return left <= right; } }