Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e9ce3a8

Browse files
add comments
1 parent 5d84173 commit e9ce3a8

File tree

1 file changed

+7
-3
lines changed
  • src/main/java/ir/sk/algorithm/others

1 file changed

+7
-3
lines changed

‎src/main/java/ir/sk/algorithm/others/Sort.java‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,19 +402,23 @@ private static int[] myMerge(int[] leftArray, int[] rightArray) {
402402
@DivideAndConquer
403403
public static void quickSort(int[] array, int start, int end) {
404404
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);
408408
}
409409

410410
/**
411411
* @return index of pivot
412412
*/
413413
public static int partition(int[] array, int start, int end) {
414414
int pivot = array[end]; // Pick pivot point
415+
416+
// Index of smaller element and indicates
417+
// the right position of pivot found so far
415418
int i = start - 1;
416419

417420
for (int j = start; j <= end - 1; j++) {
421+
// If current element is smaller than the pivot
418422
if (array[j] < pivot) {
419423
i++;
420424
array[j] = Utils.gSwap(array[i], array[i] = array[j]);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /