/** * Place the array in max-heap order. Note that the array is not fully sorted. */ private static <T extends Comparable<? super T>> void heapify(T[] arr) { int n = arr.length; for (int i = n / 2 - 1; i >= 0; i--) SortUtils.siftDown(arr, i, n - 1); }
/** * Place the array in max-heap order. Note that the array is not fully sorted. */ private static void heapify(double[] arr) { int n = arr.length; for (int i = n / 2 - 1; i >= 0; i--) SortUtils.siftDown(arr, i, n - 1); }
/** * Place the array in max-heap order. Note that the array is not fully sorted. */ private static void heapify(float[] arr) { int n = arr.length; for (int i = n / 2 - 1; i >= 0; i--) SortUtils.siftDown(arr, i, n - 1); }
/** * Place the array in max-heap order. Note that the array is not fully sorted. */ private static void heapify(int[] arr) { int n = arr.length; for (int i = n / 2 - 1; i >= 0; i--) SortUtils.siftDown(arr, i, n - 1); }
/** * In case of avoiding creating new objects frequently, one may check and * update the peek object directly and call this method to sort the internal * array in heap order. */ public void heapify() { if (n < k) { throw new IllegalStateException(); } SortUtils.siftDown(heap, 0, k-1); }
/** * Sorts the specified array into ascending order. */ public static <T extends Comparable<? super T>> void sort(T[] arr) { int n = arr.length; for (int i = n / 2 - 1; i >= 0; i--) SortUtils.siftDown(arr, i, n - 1); for (int i = n - 1; i > 0; i--) { SortUtils.swap(arr, 0, i); SortUtils.siftDown(arr, 0, i - 1); } } }
/** * Assimilate a new value from the stream. */ public void add(double datum) { sorted = false; if (n < k) { heap[n++] = datum; if (n == k) { sort(heap, k); } } else { n++; if (datum < heap[0]) { heap[0] = datum; SortUtils.siftDown(heap, 0, k-1); } } }
/** * Assimilate a new value from the stream. */ public void add(T datum) { sorted = false; if (n < k) { heap[n++] = datum; if (n == k) { heapify(heap); } } else { n++; if (datum.compareTo(heap[0]) < 0) { heap[0] = datum; SortUtils.siftDown(heap, 0, k-1); } } }
/** * Assimilate a new value from the stream. */ public void add(int datum) { sorted = false; if (n < k) { heap[n++] = datum; if (n == k) { heapify(heap); } } else { n++; if (datum < heap[0]) { heap[0] = datum; SortUtils.siftDown(heap, 0, k-1); } } }
/** * Assimilate a new value from the stream. */ public void add(float datum) { sorted = false; if (n < k) { heap[n++] = datum; if (n == k) { heapify(heap); } } else { n++; if (datum < heap[0]) { heap[0] = datum; SortUtils.siftDown(heap, 0, k-1); } } }