Quicksort 2 - Sorting

  • + 1 comment

    **Java Solution: **

    static void quickSort(int[] ar, int start, int end) {
                if (start < end) {
                int pi = partition(ar, start, end);
                quickSort(ar, start, pi - 1);
                quickSort(ar, pi + 1, end);
                printArray(ar, start, end);
                }
    	    }
            
            static int partition(int[] ar, int start, int end) {
                int pivot = ar[start];
                int i = start + 1, j = end;
                
                while (i <= j) {
                    if (ar[i] > pivot && ar[j] < pivot) {
                        int temp = ar[i];
                        ar[i] = ar[j];
                        ar[j] = temp;
                    }
                    
                    while (i <= end && ar[i] < pivot) i++;
                    while (j > start && ar[j] > pivot) j--;
                }
                ar[start] = ar[j];
                ar[j] = pivot;
                return j;
            }