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 175 176 177 178 179 180 181
| package dsa.structure.ds703_heap;
import java.util.Arrays;
public class Heap { public void heapSort1(int[] array) { genHeapByShiftUp(array);
int len = array.length; while (len > 1) { swap(array, 0, --len); shiftDown(array, 0, len); } }
public void heapSort2(int[] array) { genHeapByShiftDown(array);
int len = array.length; while (len > 1) { swap(array, 0, --len); shiftDown(array, 0, len); } }
private void genHeapByShiftUp(int[] arr) { int len = arr.length; for (int i = 1; i < len; i++) { shiftUp(arr, i); } }
private void genHeapByShiftDown(int[] arr) { int len = arr.length; for (int i = (len - 2) / 2; i >= 0; i--) { shiftDown(arr, i, len); } }
public 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 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 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) { HeapTest.testRandomExample(t -> new Heap().heapSort1(t));
HeapTest.testRandomExample(t -> new Heap().heapSort2(t)); }
public static class HeapTest { public static interface HeapFunc { void apply(int[] array); }
private static void testHeap(int[] array, HeapFunc f) { int[] mybak = Arrays.copyOf(array, array.length); Arrays.sort(mybak);
f.apply(array);
int[] result = array; int[] expect = mybak;
TestUtils.check("堆", result, expect); }
private static void testRandomExample(HeapFunc f) { for (int i = 0; i < 36; i++) { testHeap(random(9), f); testHeap(random(10), f); } }
private static void testFixedExample(HeapFunc f) { int[] array = new int[] { 310, 78, 237, 773, 96, 165, 70, 757, 665, 508 };
testHeap(array, f); }
public 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; } }
public static class TestUtils {
public static boolean check(String name, Object result, Object expect) { boolean ok = java.util.Objects.deepEquals(result, expect); printCheck(name, toString(result), toString(expect), ok); return ok; }
private static String toString(Object o) { if (o instanceof Object[]) { return java.util.Arrays.deepToString((Object[]) o); } else if (o instanceof int[]) { return java.util.Arrays.toString((int[]) o); } else if (o instanceof double[]) { return java.util.Arrays.toString((double[]) o); } else if (o instanceof char[]) { return java.util.Arrays.toString((char[]) o); } else { return java.util.Objects.toString(o); } }
public static void printCheck(String name, String result, String expect, boolean ok) { System.out.println(); System.out.println("--->");
System.out.println("result: " + result); System.out.println("expect: " + expect);
System.out.println(); if (ok) { System.out.println("比较结果为: 正确(right)"); } else { System.out.println("比较结果为: 错误(error)"); }
System.out.println("<---"); System.out.println();
if (!ok) { throw new RuntimeException(String.format("ERROR: [%s]的测试失败", name)); } } } }
|