11package com .thealgorithms .sorts ;
2-
32/**
4- * @author Varun Upadhyay (https://github.com/varunu28)
5- * @author Podshivalov Nikita (https://github.com/nikitap492)
3+ * QuickSort is a divide-and-conquer sorting algorithm.
4+ *
5+ * <p>The algorithm selects a pivot element and partitions the array into two
6+ * subarrays such that:
7+ * <ul>
8+ * <li>Elements smaller than the pivot are placed on the left</li>
9+ * <li>Elements greater than the pivot are placed on the right</li>
10+ * </ul>
11+ *
12+ * <p>The subarrays are then recursively sorted until the entire array is ordered.
13+ *
14+ * <p>This implementation uses randomization to reduce the probability of
15+ * encountering worst-case performance on already sorted inputs.
16+ *
17+ * <p>Time Complexity:
18+ * <ul>
19+ * <li>Best Case: O(n log n)</li>
20+ * <li>Average Case: O(n log n)</li>
21+ * <li>Worst Case: O(n^2)</li>
22+ * </ul>
23+ *
24+ * <p>Space Complexity: O(log n) due to recursion stack (in-place sorting).
25+ *
26+ * @author Varun Upadhyay
27+ * @author Podshivalov Nikita
628 * @see SortAlgorithm
729 */
30+
831class QuickSort implements SortAlgorithm {
932
10- /**
11- * Generic Quick Sort algorithm.
12- *
13- * Time Complexity:
14- * - Best case: O(n log n) – pivot splits array roughly in half each time.
15- * - Average case: O(n log n)
16- * - Worst case: O(n^2) – occurs when pivot consistently produces unbalanced splits.
17- *
18- * Space Complexity: O(log n) – recursion stack, in-place sorting.
19- *
20- * @see SortAlgorithm
21- */
2233 @ Override
2334 public <T extends Comparable <T >> T [] sort (T [] array ) {
2435 doSort (array , 0 , array .length - 1 );
@@ -28,27 +39,33 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
2839 /**
2940 * The sorting process
3041 *
42+ * @param array The array to be sorted
3143 * @param left The first index of an array
3244 * @param right The last index of an array
33- * @param array The array to be sorted
3445 */
3546 private static <T extends Comparable <T >> void doSort (T [] array , final int left , final int right ) {
47+ // Continue sorting only if the subarray has more than one element
3648 if (left < right ) {
49+ // Randomly choose a pivot and partition the array
3750 final int pivot = randomPartition (array , left , right );
51+ // Recursively sort elements before the pivot
3852 doSort (array , left , pivot - 1 );
53+ // Recursively sort elements after the pivot
3954 doSort (array , pivot , right );
4055 }
4156 }
4257
4358 /**
44- * Randomize the array to avoid the basically ordered sequences
59+ * Randomizes the array to avoid already ordered or nearly ordered sequences
4560 *
4661 * @param array The array to be sorted
4762 * @param left The first index of an array
4863 * @param right The last index of an array
4964 * @return the partition index of the array
5065 */
5166 private static <T extends Comparable <T >> int randomPartition (T [] array , final int left , final int right ) {
67+ // Randomizing the pivot helps avoid worst-case performance
68+ // for already sorted or nearly sorted arrays
5269 final int randomIndex = left + (int ) (Math .random () * (right - left + 1 ));
5370 SortUtils .swap (array , randomIndex , right );
5471 return partition (array , left , right );
@@ -59,20 +76,26 @@ private static <T extends Comparable<T>> int randomPartition(T[] array, final in
5976 *
6077 * @param array The array to be sorted
6178 * @param left The first index of an array
62- * @param right The last index of an array Finds the partition index of an
79+ * @param right The last index of an array
6380 * array
6481 */
6582 private static <T extends Comparable <T >> int partition (T [] array , int left , int right ) {
6683 final int mid = (left + right ) >>> 1 ;
84+ // Choose the middle element as the pivot
6785 final T pivot = array [mid ];
68-
86+ // Move the left and right pointers towards each other
6987 while (left <= right ) {
88+ // Move left pointer until an element >= pivot is found
7089 while (SortUtils .less (array [left ], pivot )) {
7190 ++left ;
7291 }
92+
93+ // Move right pointer until an element <= pivot is found
7394 while (SortUtils .less (pivot , array [right ])) {
7495 --right ;
7596 }
97+
98+ // Swap elements that are on the wrong side of the pivot
7699 if (left <= right ) {
77100 SortUtils .swap (array , left , right );
78101 ++left ;
0 commit comments