11package com .thealgorithms .sorts ;
22
33/**
4- * @author Varun Upadhyay (https://github.com/varunu28)
5- * @author Podshivalov Nikita (https://github.com/nikitap492)
4+ * QuickSort is a divide-and-conquer sorting algorithm.
5+ *
6+ * <p>The algorithm selects a pivot element and partitions the array into two
7+ * subarrays such that:
8+ * <ul>
9+ * <li>Elements smaller than the pivot are placed on the left</li>
10+ * <li>Elements greater than the pivot are placed on the right</li>
11+ * </ul>
12+ *
13+ * <p>The subarrays are then recursively sorted until the entire array is ordered.
14+ *
15+ * <p>This implementation uses randomization to reduce the probability of
16+ * encountering worst-case performance on already sorted inputs.
17+ *
18+ * <p>Time Complexity:
19+ * <ul>
20+ * <li>Best Case: O(n log n)</li>
21+ * <li>Average Case: O(n log n)</li>
22+ * <li>Worst Case: O(n^2)</li>
23+ * </ul>
24+ *
25+ * <p>Space Complexity: O(log n) due to recursion stack (in-place sorting).
26+ *
27+ * @author Varun Upadhyay
28+ * @author Podshivalov Nikita
629 * @see SortAlgorithm
730 */
31+
832class QuickSort implements SortAlgorithm {
933
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- */
2234 @ Override
2335 public <T extends Comparable <T >> T [] sort (T [] array ) {
2436 doSort (array , 0 , array .length - 1 );
@@ -28,27 +40,33 @@ public <T extends Comparable<T>> T[] sort(T[] array) {
2840 /**
2941 * The sorting process
3042 *
31- * @param left The first index of an array
32- * @param right The last index of an array
3343 * @param array The array to be sorted
44+ * @param left The first index of an array
45+ * @param right The last index of an array
3446 */
3547 private static <T extends Comparable <T >> void doSort (T [] array , final int left , final int right ) {
48+ // Continue sorting only if the subarray has more than one element
3649 if (left < right ) {
50+ // Randomly choose a pivot and partition the array
3751 final int pivot = randomPartition (array , left , right );
52+ // Recursively sort elements before the pivot
3853 doSort (array , left , pivot - 1 );
54+ // Recursively sort elements after the pivot
3955 doSort (array , pivot , right );
4056 }
4157 }
4258
4359 /**
44- * Randomize the array to avoid the basically ordered sequences
60+ * Randomizes the array to avoid already ordered or nearly ordered sequences
4561 *
4662 * @param array The array to be sorted
47- * @param left The first index of an array
63+ * @param left The first index of an array
4864 * @param right The last index of an array
4965 * @return the partition index of the array
5066 */
5167 private static <T extends Comparable <T >> int randomPartition (T [] array , final int left , final int right ) {
68+ // Randomizing the pivot helps avoid worst-case performance
69+ // for already sorted or nearly sorted arrays
5270 final int randomIndex = left + (int ) (Math .random () * (right - left + 1 ));
5371 SortUtils .swap (array , randomIndex , right );
5472 return partition (array , left , right );
@@ -58,21 +76,26 @@ private static <T extends Comparable<T>> int randomPartition(T[] array, final in
5876 * This method finds the partition index for an array
5977 *
6078 * @param array The array to be sorted
61- * @param left The first index of an array
62- * @param right The last index of an array Finds the partition index of an
63- * array
79+ * @param left The first index of an array
80+ * @param right The last index of an 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