/** * 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; } }
/** * 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--); } }
/** * 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; } }
/** * 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 } }
/** * 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; } }
/** * 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 } }
/** * 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 } }
/** * 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; } }
/** * 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(double[] 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; } }
/** * 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(int[] 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; } }
/** * 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 <T extends Comparable<? super T>> void siftUp(T[] arr, int k) { while (k > 1 && arr[k/2].compareTo(arr[k]) < 0) { swap(arr, k, k/2); k = k/2; } }
/** * 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 <T extends Comparable<? super T>> void siftDown(T[] arr, int k, int n) { while (2*k <= n) { int j = 2 * k; if (j < n && arr[j].compareTo(arr[j + 1]) < 0) { j++; } if (arr[k].compareTo(arr[j]) >= 0) { break; } SortUtils.swap(arr, k, j); k = j; } } }
/** * Sorts the specified array into ascending numerical order. */ public static void sort(float[] 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); } }
/** * Sorts the specified array into ascending numerical order. */ public static void sort(int[] 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); } }
/** * 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); } } }
/** * Sorts the specified array into ascending numerical order. */ public static void sort(double[] 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); } }