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
| import java.util.*;
public class HeapSort { public static void main(String[] args) { int times = 100_0000, maxArraySize = 1000; Random r = new Random(); while(times-- > 0) { int len = r.nextInt(maxArraySize); int[] arr1 = new int[len], arr2 = new int[len]; for(int i = 0; i < len; ++i) arr1[i] = arr2[i] = r.nextInt(21) - 10; Arrays.sort(arr1); heapSort(arr2); for(int i = 0; i < len; ++i) { if(arr1[i] != arr2[i]) { for(i = 0; i < len; ++i) System.out.print(arr1[i] + ","); System.out.println(); for(i = 0; i < len; ++i) System.out.print(arr2[i] + ","); System.out.println(); throw new RuntimeException(); } } } } public static void heapSort(int[] arr) { int i,len = arr.length; for(i = (len >> 1) - 1; i > -1; --i) heapIfy(arr,len,i); for(i = len; i > 1;) { swap(arr,0,--i); heapIfy(arr,i,0); } } public static void heapIfy(int[] heap, int size, int index) { int child, bound = size >> 1, v = heap[index]; while(index < bound) { child = (index << 1) + 1; if(child + 1 < size && heap[child + 1] > heap[child]) ++child; if(v >= heap[child]) break; heap[index] = heap[child]; index = child; } heap[index] = v; } public static void swap(int[] arr, int i, int j){ int t = arr[i]; arr[i] = arr[j]; arr[j] = t; } }
|