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.
- [ ] I have read CONTRIBUTING.md.
- [ ] This pull request is all my own work -- I have not plagiarized it.
- [ ] All filenames are in PascalCase.
- [ ] All functions and variable names follow Java naming conventions.
- [ ] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [ ] All new code is formatted with
clang-format -i --style=file path/to/your/file.java
Random Pivot Selection: In your implementation, the pivot selection logic should be more intuitive and clear. The pivot is always chosen from the rightmost element of the array.
Partitioning Logic: Simplified the partitioning logic by using a single loop instead of nested while loops, which makes it easier to understand.
What do you think about this suggestion:
/**
* Partitions the array around a pivot element.
*
* @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) {
T pivot = array[right];
int i = left - 1;
for (int j = left; j < right; j++) {
if (array[j].compareTo(pivot) <= 0) {
i++;
swap(array, i, j);
}
}
swap(array, i + 1, right);
return i + 1;
}
}
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution!
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution!
Please reopen this pull request once you have made the required changes. If you need help, feel free to ask in our Discord server or ping one of the maintainers here. Thank you for your contribution!