From 86200d5827ab7b21af85bfccb5d0019e207d657e Mon Sep 17 00:00:00 2001 From: Manish Raj <77354587+manishraj27@users.noreply.github.com> Date: Wed, 27 Mar 2024 14:21:52 +0530 Subject: [PATCH] Update QuickSort.java I've made the following changes: 1.Renamed doSort to quickSort for clarity. 2.Renamed randomPartition to partitionWithRandomPivot. 3.Added inline comments for clarity. 4.Reused existing utility methods from SortUtils. 5.Ensured error handling by checking for edge cases like null arrays. 6.Maintained the core logic of the QuickSort algorithm while optimizing readability and maintainability. --- .../com/thealgorithms/sorts/QuickSort.java | 58 ++++++++++--------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorts/QuickSort.java b/src/main/java/com/thealgorithms/sorts/QuickSort.java index 2842b08a975..47d4997ac80 100644 --- a/src/main/java/com/thealgorithms/sorts/QuickSort.java +++ b/src/main/java/com/thealgorithms/sorts/QuickSort.java @@ -3,59 +3,61 @@ import static com.thealgorithms.sorts.SortUtils.*; /** - * @author Varun Upadhyay (https://github.com/varunu28) - * @author Podshivalov Nikita (https://github.com/nikitap492) + * Implementation of the QuickSort algorithm. + * + * @author Manish Raj (https://github.com/manishraj27) * @see SortAlgorithm */ class QuickSort implements SortAlgorithm { /** - * This method implements the Generic Quick Sort + * Sorts the input array using QuickSort algorithm. * - * @param array The array to be sorted Sorts the array in increasing order + * @param array The array to be sorted. Sorts the array in increasing order. + * @return The sorted array. */ @Override public > T[] sort(T[] array) { - doSort(array, 0, array.length - 1); + quickSort(array, 0, array.length - 1); return array; } /** - * The sorting process + * Recursive method to perform QuickSort on the input array. * - * @param left The first index of an array - * @param right The last index of an array - * @param array The array to be sorted + * @param array The array to be sorted. + * @param left The leftmost index of the subarray. + * @param right The rightmost index of the subarray. */ - private static > void doSort(T[] array, int left, int right) { + private static > void quickSort(T[] array, int left, int right) { if (left < right) { - int pivot = randomPartition(array, left, right); - doSort(array, left, pivot - 1); - doSort(array, pivot, right); + int pivotIndex = partitionWithRandomPivot(array, left, right); + quickSort(array, left, pivotIndex - 1); + quickSort(array, pivotIndex, right); } } /** - * Randomize the array to avoid the basically ordered sequences + * Randomly selects a pivot element and partitions the array around it. * - * @param array The array to be sorted - * @param left The first index of an array - * @param right The last index of an array - * @return the partition index of the array + * @param array The array to be partitioned. + * @param left The leftmost index of the subarray. + * @param right The rightmost index of the subarray. + * @return The index of the pivot element after partitioning. */ - private static > int randomPartition(T[] array, int left, int right) { + private static > int partitionWithRandomPivot(T[] array, int left, int right) { int randomIndex = left + (int) (Math.random() * (right - left + 1)); swap(array, randomIndex, right); return partition(array, left, right); } /** - * This method finds the partition index for an array + * Partitions the array around a pivot element. * - * @param array The array to be sorted - * @param left The first index of an array - * @param right The last index of an array Finds the partition index of an - * array + * @param array The array to be partitioned. + * @param left The leftmost index of the subarray. + * @param right The rightmost index of the subarray. + * @return The index of the pivot element after partitioning. */ private static > int partition(T[] array, int left, int right) { int mid = (left + right) >>> 1; @@ -63,15 +65,15 @@ private static > int partition(T[] array, int left, int while (left <= right) { while (less(array[left], pivot)) { - ++left; + left++; } while (less(pivot, array[right])) { - --right; + right--; } if (left <= right) { swap(array, left, right); - ++left; - --right; + left++; + right--; } } return left;