/* * Sort an array of ints using quicksort. */ public static void quicksort(int [] a, int left, int right) { /* Base case */ if (left >= right) return; /* Select pivot and place it in rightmost location */ int pivot = A[right]; /* This selects rightmost as pivot. POOR CHOICE. */ int i=0, j = right-1; while (i <= j) { /* Advance i to point to a value larger than the pivot */ while (i < right && a[i] <= pivot) i ++; /* Decrement j to point to a value smaller than pivot */ while (j >= 0 && a[j] >= pivot) j --; /* If pointers have not crossed, swap A[i] and A[j]. */ if (i < j) { int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } } /* Swap A[i] with the pivot */ tmp = A[i]; A[i] = A[right]; A[right] = tmp; /* Recurse on sub-arrays */ quicksort (A, left, i-1); quicksort (A, i+1, right); }