Skip to content

Optimise quick_sort #678

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

Merged
merged 1 commit into from
Mar 18, 2024
Merged

Optimise quick_sort #678

merged 1 commit into from
Mar 18, 2024

Conversation

genskyff
Copy link
Contributor

@genskyff genskyff commented Feb 14, 2024

Pull Request Template

Description

If the input array is completely ordered, let's say the length of the subarray in the recursion is m, each partition operation will result in a left subarray of length 0 and a right subarray of length m - 1. In this scenario, the height of the recursion tree will reach n - 1, requiring O(n) size of stack space.

To avoid this, it's possible to compare the lengths of the two subarrays each time and only recurse on the shorter subarray. Since the length of the shorter subarray won't exceed n / 2, this approach ensures that the recursion depth does not go beyond log(n), thereby optimizing the worst-case space complexity to log(n).

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

@codecov-commenter
Copy link

codecov-commenter commented Feb 14, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.71%. Comparing base (06a56e9) to head (91b64fb).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #678   +/-   ##
=======================================
  Coverage   94.71%   94.71%           
=======================================
  Files         293      293           
  Lines       23346    23351    +5     
=======================================
+ Hits        22113    22118    +5     
  Misses       1233     1233           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

This pull request has been automatically marked as abandoned because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale label Mar 16, 2024
@vil02 vil02 removed the stale label Mar 18, 2024
@vil02 vil02 changed the title update quick_sort Optimise quick_sort Mar 18, 2024
@vil02 vil02 merged commit 9572098 into TheAlgorithms:master Mar 18, 2024
@genskyff genskyff deleted the perf/update-quick-sort branch March 19, 2024 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants