-
Notifications
You must be signed in to change notification settings - Fork 19.9k
Update QuickSort.java #5089
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
Closed
Closed
Update QuickSort.java #5089
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 { | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* 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); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||
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)); | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
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; | ||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.