/** * Sorts the specified array into ascending numerical order. * @return the original index of elements after sorting in range [0, n). */ public static int[] sort(int[] arr) { int[] order = new int[arr.length]; for (int i = 0; i < order.length; i++) { order[i] = i; } sort(arr, order); return order; }
/** * Find the median of an array of type integer. */ public static int median(int[] a) { int k = a.length / 2; return select(a, k); }
/** * Find the third quantile (p = 3/4) of an array of type double. The input array will * be rearranged. */ public static <T extends Comparable<? super T>> T q3(T[] a) { return QuickSelect.q3(a); }
/** * 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 <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); }
/** * Insert a new item into queue. * @param v the index of item. */ public void insert(int v) { pq[++n] = v; qp[v] = n; swim(n); }
/** * Sorts the specified array into ascending order. * @return the original index of elements after sorting in range [0, n). */ public static <T extends Comparable<? super T>> int[] sort(T[] arr) { int[] order = new int[arr.length]; for (int i = 0; i < order.length; i++) { order[i] = i; } sort(arr, order); return order; }
/** * Find the third quantile (p = 3/4) of an array of type float. */ public static float q3(float[] a) { int k = 3 * a.length / 4; return select(a, k); }
/** * 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 } }
/** * Besides sorting the array arr, the array brr will be also * rearranged as the same order of arr. */ public static void sort(int[] arr, Object[] brr) { sort(arr, brr, arr.length); }
/** * Find the median of an array of type double. */ public static double median(double[] a) { int k = a.length / 2; return select(a, k); }
/** * 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--); } }
/** * Besides sorting the array arr, the array brr will be also * rearranged as the same order of arr. */ public static void sort(float[] arr, Object[] brr) { sort(arr, brr, arr.length); }
/** * Find the third quantile (p = 3/4) of an array of type integer. */ public static int q3(int[] a) { int k = 3 * a.length / 4; return select(a, k); }
/** * This is an effecient implementation Quick Sort algorithm without * recursive. Besides sorting the array arr, the array brr will be also * rearranged as the same order of arr. */ public static void sort(double[] arr, double[] brr) { sort(arr, brr, arr.length); }
/** * Find the third quantile (p = 3/4) of an array of type double. */ public static <T extends Comparable<? super T>> T q3(T[] a) { int k = 3 * a.length / 4; return select(a, k); } }