冒泡排序
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static int[] maopaoSort(int[] arr) { for (int i = 0; i < arr.length - 1; i++) { for (int j = 0; j < arr.length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; }
|
选择排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
public static int[] xuanzeSort(int[] arr) { int minIndex = 0; for (int i = 0; i < arr.length - 1; i++) { minIndex = i; for (int j = i + 1; j < arr.length; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } if (minIndex != i) { int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } } return arr; }
|
插入排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
public static int[] charuSort(int[] arr) { for (int i = 1; i < arr.length; i++) { int j = i; int target = arr[i]; while (j > 0 && target < arr[j - 1]) { arr[j] = arr[j - 1]; j--; } arr[j] = target; } return arr; }
|
快速排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
public static int[] kuaisuSort(int[] arr) { int len = arr.length; int left = 0; int right = len - 1; kuaisuSortCore(arr, left, right); return arr; }
public static void kuaisuSortCore(int[] arr, int left, int right) { if (left > right) { return; } int base = arr[left]; int i = left, j = right; while (i != j) { while (arr[j] >= base && i < j) { j--; } while (arr[i] <= base && i < j) { i++; } if (i < j) { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } arr[left] = arr[i]; arr[i] = base; sort(arr, left, i - 1); sort(arr, i + 1, right); }
|
堆排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
private static int[] duiSort(int[] arr) { for (int i = (arr.length - 1) / 2; i >= 0; i--) { duiSortCore(arr, i, arr.length); } for (int i = arr.length - 1; i > 0; i--) { int temp = arr[i]; arr[i] = arr[0]; arr[0] = temp; duiSortCore(arr, 0, i); } return arr; }
private static void duiSortCore(int[] arr, int parent, int length) { int temp = arr[parent]; int left = 2 * parent + 1; while (left < length) { int right = left + 1; if (right < length && arr[left] < arr[right]) { left++; } if (temp >= arr[left]) { break; } arr[parent] = arr[left]; parent = left; left = 2 * left + 1; } arr[parent] = temp; }
|
希尔排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
public static int[] xierSort(int[] arr) { int d = arr.length / 2; while (d >= 1) { xierSortCore(arr, d); d /= 2; } return arr; }
public static void xierSortCore(int[] arr, int d) { for (int i = d; i < arr.length; i++) { int j = i - d; int temp = arr[i]; while (j >= 0 && arr[j] > temp) { arr[j + d] = arr[j]; j -= d; } if (j != i - d) { arr[j + d] = temp; } } }
|
并归排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
public static int[] bingguiSort(int[] arr) { bingguiSortCore(arr, 0, arr.length - 1); return arr; }
public static void bingguiSortCore(int[] arr, int left, int right) { if (left >= right){ return; } int mid = (left + right) / 2; bingguiSortCore(arr, left, mid); bingguiSortCore(arr, mid + 1, right); int[] temp = new int[right - left + 1]; int i = left; int j = mid + 1; int k = 0; while (i <= mid && j <= right) { if (arr[i] <= arr[j]) { temp[k++] = arr[i++]; } else { temp[k++] = arr[j++]; } } while (i <= mid) { temp[k++] = arr[i++]; } while (j <= right) { temp[k++] = arr[j++]; } for (int p = 0; p < temp.length; p++) { arr[left + p] = temp[p]; } } # 计数排序 ```Java
public static int[] jishuSort(int[] arr) { int max = Integer.MIN_VALUE; for (int i : arr) { if (i > max){ max = i; } } int[] count = new int[max + 1]; Arrays.fill(count, 0); for (int i = 0; i < arr.length; i++) { count[arr[i]]++; } int k = 0; for (int i = 0; i <= max; i++) { for (int j = 0; j < count[i]; j++) { arr[k++] = i; } } return arr; }
|
桶排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
public static int[] tongSort(int[] arr) { int bucketNums = 10; List<List<Integer>> buckets = new ArrayList<List<Integer>>(); for (int i = 0; i < 10; i++) { buckets.add(new LinkedList<Integer>()); } for (int i = 0; i < arr.length; i++) { buckets.get(arr[i] / 10).add(arr[i]); } for (int i = 0; i < buckets.size(); i++) { if (!buckets.get(i).isEmpty()) { Collections.sort(buckets.get(i)); } } int k = 0; for (List<Integer> bucket : buckets) { for (int ele : bucket) { arr[k++] = ele; } } return arr; }
|
基数排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
public static int[] baseSort(int[] arr) { int maxBit = Integer.MIN_VALUE; for (int ele : arr) { int len = (ele + "").length(); if (len > maxBit){ maxBit = len; } } for (int i = 1; i <= maxBit; i++) { List<List<Integer>> buf = new ArrayList<List<Integer>>(); for (int n = 0; n < 10; n++) { buf.add(new LinkedList<Integer>()); } for (int m = 0; m < arr.length; m++) { int result = 0; String sx = arr[m] + ""; if (sx.length() < i){ result = 0; } else{ result = sx.charAt(sx.length() - i) - '0'; } buf.get(result).add(arr[m]); } int k = 0; for (List<Integer> bucket : buf) { for (int ele : bucket) { arr[k++] = ele; } } } return arr; }
|
所有排序测试
public static void main(String[] args) {
int[] arr1 = new int[]{1, 5, 6, 2, 3, 7, 4};
int[] result1 = maopaoSort(arr1);
System.out.println("冒泡排序:" + Arrays.toString(result1));
int[] arr2 = new int[]{1, 5, 6, 2, 3, 7, 4};
int[] result2 = xuanzeSort(arr2);
System.out.println("选择排序:" + Arrays.toString(result2));
int[] arr3 = new int[]{1, 5, 6, 2, 3, 7, 4};
int[] result3 = charuSort(arr3);
System.out.println("插入排序:" + Arrays.toString(result3));
int[] arr4 = new int[]{1, 5, 6, 2, 3, 7, 4};
int[] result4 = kuaisuSort(arr4);
System.out.println("快速排序:" + Arrays.toString(result4));
int[] arr5 = new int[]{1, 5, 6, 2, 3, 7, 4};
int[] result5 = xierSort(arr5);
System.out.println("希尔排序:" + Arrays.toString(result5));
int[] arr6 = new int[]{1, 5, 6, 2, 3, 7, 4};
int[] result6 = bingguiSort(arr6);
System.out.println("并归排序:" + Arrays.toString(result6));
int[] arr7 = new int[]{1, 5, 6, 2, 3, 7, 4};
int[] result7 = jishuSort(arr7);
System.out.println("计数排序:" + Arrays.toString(result7));
int[] arr8 = new int[]{1, 5, 6, 2, 3, 7, 4};
int[] result8 = baseSort(arr8);
System.out.println("基数排序:" + Arrays.toString(result8));
int[] arr9 = new int[]{1, 5, 6, 2, 3, 7, 4};
int[] result9 = duiSort(arr9);
System.out.println("堆排序:" + Arrays.toString(result9));
int[] arr10 = new int[]{1, 5, 6, 2, 3, 7, 4};
int[] result10 = tongSort(arr10);
System.out.println("桶排序:" + Arrays.toString(result10));
}
```