public class QuickSort {

    private static int partition(int[] arr, int start, int end) {
        int pivot = arr[end];
        int p_idx = start - 1;
        int tmp;
        for (int i = start; i < end; ++i) {
            if (arr[i] <= pivot) {
                p_idx++;
                tmp = arr[i];
                arr[i] = arr[p_idx];
                arr[p_idx] = tmp;
            }
        }
        tmp = arr[p_idx + 1];
        arr[p_idx + 1] = arr[end];
        arr[end] = tmp;
        return p_idx + 1;
    }

    public static void quickSort(int[] a, int left, int right) {
        if (left < right) {
            int pi = partition(a, left, right);  //pi index of pivot
            quickSort(a, left, pi - 1);  //sort left of pivot
            quickSort(a, pi + 1, right);  //sort right of pivot
        }
    }

    public static void main(String[] args) {
        int[] arr = new int[] {2, 4, 2, 6, 7, -1};
        quickSort(arr, 0, arr.length - 1);
        for (int element : arr) {
            System.out.println(element);
        }
    }
}

@@ -46,127 +46,1023 @@
 int 
-partition(int%5B%5D arr, int start, int end) %7B%0A        int pivot = arr%5Bend%5D;%0A        int p_idx = start - 1;%0A        int tmp
+compare(int%5B%5D arr, int i, int j) %7B%0A        if (arr%5Bi%5D %3E arr%5Bj%5D) %7B%0A            return i;%0A        %7D else %7B%0A            return j;%0A        %7D%0A    %7D%0A%0A    /*%0A    This method returns the index of median of array at i, j, k.%0A    @param arr: Input array to be sorted.%0A    @param i: Left end index of array.%0A    @param j: Right end index of array.%0A    @param k: Middle index of array.%0A    @return: Index of median of array at i, j, k.%0A    */%0A    private static int median(int%5B%5D arr, int i, int j, int k) %7B%0A        if (arr%5Bi%5D %3E arr%5Bj%5D && arr%5Bi%5D %3E arr%5Bk%5D) %7B%0A            return compare(arr, k, j);%0A        %7D else if (arr%5Bj%5D %3E arr%5Bi%5D && arr%5Bj%5D %3E arr%5Bk%5D) %7B%0A            return compare(arr, k, i);%0A        %7D else %7B%0A            return compare(arr, i, j);%0A        %7D%0A    %7D%0A%0A    private static int partition(int%5B%5D arr, int start, int end) %7B%0A        int l = median(arr, start, end, (start + end) / 2);%0A        int p_idx = start - 1;%0A        int tmp = arr%5Bl%5D;%0A        arr%5Bl%5D = arr%5Bend%5D;%0A        arr%5Bend%5D = tmp;%0A        int pivot = arr%5Bend%5D
 ;%0A  
@@ -1280,32 +1280,49 @@
     %7D%0A        %7D%0A
+        p_idx++;%0A
         tmp = ar
@@ -1328,20 +1328,16 @@
 rr%5Bp_idx
- + 1
 %5D;%0A     
@@ -1348,20 +1348,16 @@
 rr%5Bp_idx
- + 1
 %5D = arr%5B
@@ -1406,20 +1406,16 @@
 rn p_idx
- + 1
 ;%0A    %7D%0A

Fix #435: Randomize pivot for Quicksort [Java] (#447)