Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update QuickSort.java #5089

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions src/main/java/com/thealgorithms/sorts/QuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,77 @@
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 {

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
private static final int RANDOM_BOUND = 1;
/**

* 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 extends Comparable<T>> T[] sort(T[] array) {
doSort(array, 0, array.length - 1);
quickSort(array, 0, array.length - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error Handling: You should add a check to handle cases where the array is null or has only one element, returning the array directly in such cases.

Suggested change
quickSort(array, 0, array.length - 1);
if (array == null || array.length <= 1) {
return array;
}
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 <T extends Comparable<T>> void doSort(T[] array, int left, int right) {
private static <T extends Comparable<T>> 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 <T extends Comparable<T>> int randomPartition(T[] array, int left, int right) {
private static <T extends Comparable<T>> int partitionWithRandomPivot(T[] array, int left, int right) {
int randomIndex = left + (int) (Math.random() * (right - left + 1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int randomIndex = left + (int) (Math.random() * (right - left + 1));
int randomIndex = left + (int) (Math.random() * (right - left + RANDOM_BOUND));

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 <T extends Comparable<T>> int partition(T[] array, int left, int right) {
int mid = (left + right) >>> 1;
T pivot = array[mid];

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;
Expand Down