/** * Reverses the order of the elements in the specified array. * @param a an array to reverse. */ public static <T> void reverse(T[] a) { int i = 0, j = a.length - 1; while (i < j) { SortUtils.swap(a, i++, j--); } }
/** * 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); }
/** * To restore the max-heap condition when a node's priority is increased. * We move up the heap, exchaning the node at position k with its parent * (at postion k/2) if necessary, continuing as long as a[k/2] < a[k] or * until we reach the top of the heap. */ public static void siftUp(float[] arr, int k) { while (k > 1 && arr[k/2] < arr[k]) { swap(arr, k, k/2); k = k/2; } }
/** * 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); }
/** * Reverses the order of the elements in the specified array. * @param a an array to reverse. */ public static void reverse(double[] a) { int i = 0, j = a.length - 1; while (i < j) { SortUtils.swap(a, i++, j--); // code for swap not shown, but easy enough } }
/** * 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); }
/** * To restore the max-heap condition when a node's priority is increased. * We move up the heap, exchaning the node at position k with its parent * (at postion k/2) if necessary, continuing as long as a[k/2] < a[k] or * until we reach the top of the heap. */ public static void siftUp(double[] arr, int k) { while (k > 1 && arr[k/2] < arr[k]) { swap(arr, k, k/2); k = k/2; } }
/** * 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); }
/** * To restore the max-heap condition when a node's priority is increased. * We move up the heap, exchaning the node at position k with its parent * (at postion k/2) if necessary, continuing as long as a[k/2] < a[k] or * until we reach the top of the heap. */ public static void siftUp(int[] arr, int k) { while (k > 1 && arr[k/2] < arr[k]) { swap(arr, k, k/2); k = k/2; } }
/** * 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); } } }
/** * 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); }
/** * Reverses the order of the elements in the specified array. * @param a an array to reverse. */ public static void reverse(int[] a) { int i = 0, j = a.length - 1; while (i < j) { SortUtils.swap(a, i++, j--); // code for swap not shown, but easy enough } }
/** * 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); } } }
/** * Reverses the order of the elements in the specified array. * @param a an array to reverse. */ public static void reverse(float[] a) { int i = 0, j = a.length - 1; while (i < j) { SortUtils.swap(a, i++, j--); // code for swap not shown, but easy enough } }
/** * 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); } } }
/** * To restore the max-heap condition when a node's priority is decreased. * We move down the heap, exchanging the node at position k with the larger * of that node's two children if necessary and stopping when the node at * k is not smaller than either child or the bottom is reached. Note that * if n is even and k is n/2, then the node at k has only one child -- this * case must be treated properly. */ public static void siftDown(float[] arr, int k, int n) { while (2*k <= n) { int j = 2 * k; if (j < n && arr[j] < arr[j + 1]) { j++; } if (arr[k] >= arr[j]) { break; } swap(arr, k, j); k = j; } }
/** * 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); } } }