Java icon indicating copy to clipboard operation
Java copied to clipboard

Update QuickSort.java

Open manishraj27 opened this issue 1 year ago • 4 comments

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

manishraj27 avatar Mar 27 '24 08:03 manishraj27

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.

sozelfist avatar Apr 16 '24 01:04 sozelfist

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;
    }
}

sozelfist avatar Apr 16 '24 01:04 sozelfist

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!

github-actions[bot] avatar May 17 '24 00:05 github-actions[bot]

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!

github-actions[bot] avatar Aug 25 '24 00:08 github-actions[bot]

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!

github-actions[bot] avatar Sep 01 '24 00:09 github-actions[bot]