package com.algorithms.sorting;

import edu.princeton.cs.algs4.StdRandom;


public class Quick {

    public void sort(Comparable[] a) {
        StdRandom.shuffle(a);
        sort(a, 0, a.length - 1);
    }

    private static void sort(Comparable[] a, int lo, int hi) {
        if (hi <= lo) return;
        int part = partition(a, lo, hi);
        sort(a, lo, part - 1);
        sort(a, part + 1, hi);
    }

    private static int partition(Comparable[] a, int lo, int hi) {
        int i = lo, j = j = hi + 1;
        while (true) {
            while (less(a[++i], a[lo])) if (i == hi) break;
            while (less(a[lo], a[--j])) if (j == lo) break;

            if (i >= j) break;
            exch(a, i, j);
        }

        exch(a, lo, j);
        return j;
    }

    private static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    private static void exch(Comparable[] a, int i, int j) {
        Comparable swap = a[i];
        a[i] = a[j];
        a[j] = swap;
    }
}

@@ -91,16 +91,344 @@
 uick %7B%0A%0A
+    public static Comparable select(Comparable%5B%5D a, int k) %7B%0A        StdRandom.shuffle(a);%0A        int lo = 0, hi = a.length-1;%0A        while (true) %7B%0A            int j = partition(a, lo, hi);%0A%0A            if (j %3C k)      lo = j+1;%0A            else if (j %3E k) hi = j-1;%0A            else            return a%5Bj%5D;%0A        %7D%0A    %7D%0A%0A
     publ

Add Quick-select