|
| 1 | +#ifndef CH2_QUICK_H |
| 2 | +#define CH2_QUICK_H |
| 3 | + |
| 4 | +#include <vector> |
| 5 | +#include <random> |
| 6 | +#include <iostream> |
| 7 | +#include <algorithm> |
| 8 | +#include <stdexcept> |
| 9 | + |
| 10 | +using std::vector; |
| 11 | +using std::swap; |
| 12 | +using std::cout; |
| 13 | +using std::endl; |
| 14 | +using std::to_string; |
| 15 | +using std::random_device; |
| 16 | +using std::mt19937; |
| 17 | +using std::shuffle; |
| 18 | +using std::runtime_error; |
| 19 | + |
| 20 | +random_device rd; |
| 21 | +mt19937 g(rd()); |
| 22 | + |
| 23 | +/** |
| 24 | + * The {@code Quick} class provides static methods for sorting an |
| 25 | + * array and selecting the ith smallest element in an array using quicksort. |
| 26 | + * <p> |
| 27 | + * For additional documentation, |
| 28 | + * see <a href="https://algs4.cs.princeton.edu/23quick">Section 2.3</a> of |
| 29 | + * <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne. |
| 30 | + * |
| 31 | + * @author Robert Sedgewick |
| 32 | + * @author Kevin Wayne |
| 33 | + */ |
| 34 | +class Quick { |
| 35 | +public: |
| 36 | + // This class should not be instantiated. |
| 37 | + Quick() = delete; |
| 38 | + |
| 39 | + /** |
| 40 | + * Rearranges the array in ascending order, using the natural order. |
| 41 | + * @param a the array to be sorted |
| 42 | + */ |
| 43 | + template<typename T> |
| 44 | + static void sort(vector<T> &a) { |
| 45 | + shuffle(a.begin(), a.end(), g); |
| 46 | + sort(a, 0, a.size() - 1); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Rearranges the array so that {@code a[k]} contains the kth smallest key; |
| 51 | + * {@code a[0]} through {@code a[k-1]} are less than (or equal to) {@code a[k]}; and |
| 52 | + * {@code a[k+1]} through {@code a[n-1]} are greater than (or equal to) {@code a[k]}. |
| 53 | + * |
| 54 | + * @param a the array |
| 55 | + * @param k the rank of the key |
| 56 | + * @return the key of rank {@code k} |
| 57 | + * @throws IllegalArgumentException unless {@code 0 <= k < a.length} |
| 58 | + */ |
| 59 | + template<typename T> |
| 60 | + static T select(vector<T> &a, int k) { |
| 61 | + if (k < 0 || k >= a.size()) { |
| 62 | + throw runtime_error("index is not between 0 and " + to_string(a.size()) + ": " + to_string(k)); |
| 63 | + } |
| 64 | + shuffle(a.begin(), a.end(), g); |
| 65 | + int lo = 0, hi = a.size() - 1; |
| 66 | + while (hi > lo) { |
| 67 | + int i = partition(a, lo, hi); |
| 68 | + if (i > k) hi = i - 1; |
| 69 | + else if (i < k) lo = i + 1; |
| 70 | + else return a[i]; |
| 71 | + } |
| 72 | + return a[lo]; |
| 73 | + } |
| 74 | + |
| 75 | + // print array to standard output |
| 76 | + template<typename T> |
| 77 | + static void show(vector<T> &a) { |
| 78 | + for (int i = 0; i < a.size(); i++) { |
| 79 | + cout << a[i] << endl; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | +private: |
| 85 | + // quicksort the subarray from a[lo] to a[hi] |
| 86 | + template<typename T> |
| 87 | + static void sort(vector<T> &a, int lo, int hi) { |
| 88 | + if (hi <= lo) return; |
| 89 | + int j = partition(a, lo, hi); |
| 90 | + sort(a, lo, j - 1); |
| 91 | + sort(a, j + 1, hi); |
| 92 | + } |
| 93 | + |
| 94 | + // partition the subarray a[lo..hi] so that a[lo..j-1] <= a[j] <= a[j+1..hi] |
| 95 | + // and return the index j. |
| 96 | + template<typename T> |
| 97 | + static int partition(vector<T> &a, int lo, int hi) { |
| 98 | + int i = lo; |
| 99 | + int j = hi + 1; |
| 100 | + T v = a[lo]; |
| 101 | + while (true) { |
| 102 | + // find item on lo to swap |
| 103 | + while (a[++i] < v) { |
| 104 | + if (i == hi) break; |
| 105 | + } |
| 106 | + |
| 107 | + // find item on hi to swap |
| 108 | + while (v < a[--j]) { |
| 109 | + if (j == lo) break; // redundant since a[lo] acts as sentinel |
| 110 | + } |
| 111 | + |
| 112 | + // check if pointers cross |
| 113 | + if (i >= j) break; |
| 114 | + |
| 115 | + swap(a[i], a[j]); |
| 116 | + } |
| 117 | + |
| 118 | + // put partitioning item v at a[j] |
| 119 | + swap(a[lo], a[j]); |
| 120 | + |
| 121 | + // now, a[lo .. j-1] <= a[j] <= a[j+1 .. hi] |
| 122 | + return j; |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +#endif //CH2_QUICK_H |
0 commit comments