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.
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 length0
and a right subarray of lengthm - 1
. In this scenario, the height of the recursion tree will reachn - 1
, requiringO(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 beyondlog(n)
, thereby optimizing the worst-case space complexity tolog(n)
.Checklist:
cargo clippy --all -- -D warnings
just before my last commit and fixed any issue that was found.cargo fmt
just before my last commit.cargo test
just before my last commit and all tests passed.COUNTRIBUTING.md
and my code follows its guidelines.