@@ -402,19 +402,23 @@ private static int[] myMerge(int[] leftArray, int[] rightArray) {
402
402
@ DivideAndConquer
403
403
public static void quickSort (int [] array , int start , int end ) {
404
404
if (end <= start ) return ; // base case
405
- int pi = partition (array , start , end );
406
- quickSort (array , start , pi - 1 );
407
- quickSort (array , pi + 1 , end );
405
+ int pivotIndex = partition (array , start , end );
406
+ quickSort (array , start , pivotIndex - 1 );
407
+ quickSort (array , pivotIndex + 1 , end );
408
408
}
409
409
410
410
/**
411
411
* @return index of pivot
412
412
*/
413
413
public static int partition (int [] array , int start , int end ) {
414
414
int pivot = array [end ]; // Pick pivot point
415
+
416
+ // Index of smaller element and indicates
417
+ // the right position of pivot found so far
415
418
int i = start - 1 ;
416
419
417
420
for (int j = start ; j <= end - 1 ; j ++) {
421
+ // If current element is smaller than the pivot
418
422
if (array [j ] < pivot ) {
419
423
i ++;
420
424
array [j ] = Utils .gSwap (array [i ], array [i ] = array [j ]);
0 commit comments