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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| package dsa.algorithm.da101_sort.st15;
import java.util.Arrays;
public class HeapSort {
public static void sort(int[] array) { heapSort1(array); }
public static void heapSort1(int[] array) { genHeapByShiftUp(array);
int len = array.length; while (len > 1) { swap(array, 0, --len); shiftDown(array, 0, len); } }
public static void heapSort2(int[] array) { genHeapByShiftDown(array);
int len = array.length; while (len > 1) { swap(array, 0, --len); shiftDown(array, 0, len); } }
private static void genHeapByShiftUp(int[] arr) { int len = arr.length; for (int i = 1; i < len; i++) { shiftUp(arr, i); } }
private static void genHeapByShiftDown(int[] arr) { int len = arr.length; for (int i = (len - 2) / 2; i >= 0; i--) { shiftDown(arr, i, len); } }
public static void shiftUp(int[] arr, int i) { int j = (i - 1) / 2; while (j >= 0) { if (arr[i] > arr[j]) { swap(arr, i, j);
i = j; j = (i - 1) / 2; } else { break; } } }
public static void shiftDown(int[] arr, int i, int heapSize) { int j = 2 * i + 1; while (j < heapSize) { j = j + 1 < heapSize && arr[j + 1] > arr[j] ? j + 1 : j; if (arr[j] > arr[i]) { swap(arr, j, i);
i = j; j = 2 * i + 1; } else { break; } } }
private static void swap(int[] array, int i, int j) {
int t = array[i]; array[i] = array[j]; array[j] = t; }
public static void main(String[] args) { SortTest.testRandomExample(t -> sort(t)); }
public static class SortTest { public static interface SortFunc { void sort(int[] array); }
private static boolean testSort(int[] array, SortFunc sf) { System.out.println(); System.out.println("--->");
int[] mybak = Arrays.copyOf(array, array.length);
print(array, "数组排序前");
Arrays.sort(mybak); print(mybak, "正确结果为");
sf.sort(array); print(array, "数组排序后");
boolean ret = check(array, mybak);
System.out.println("<---"); System.out.println();
return ret; }
private static void testRandomExample(SortFunc sf) { boolean ret; for (int i = 0; i < 36; i++) { ret = testSort(random(9), sf); if (!ret) { break; }
ret = testSort(random(10), sf); if (!ret) { break; } } }
private static void testFixedExample(SortFunc sf) { int[] array = new int[] { 310, 78, 237, 773, 96, 165, 70, 757, 665, 508 };
testSort(array, sf); }
private static int[] random(int n) { int[] array = new int[n]; for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * 1000) + 1; }
return array; }
private static boolean check(int[] array, int[] bak) { boolean ok = Arrays.equals(array, bak);
System.out.println(); if (ok) { System.out.println("排序结果为: 正确(right)"); } else { System.out.println("排序结果为: 错误(error)"); }
return ok; }
private static void print(int[] array, String tip) { System.out.println(tip + ":" + Arrays.toString(array)); } } }
|