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.
Thanks for your sharing solutions.
There is a problem with your solution to the clrs-07-01-02. You can not simply count the numbers that equal to the pivot. Although it can produce the right answer when all numbers are equal. However, it is not a right partition algorithm!
Consider the test case:
[5, 2, 4, 7, 5, 1, 5, 9, 5, 1, 10, 5]
After your original partition it is:
[5, 2, 4, 5, 1, 5, 5, 1, 5, 9, 10, 7]
And the returned pivot index is 6. However, the larger part has a number 1. Obviously, it is not a proper partition algorithm.
I fix the problem by moving the smaller number to the leftmost, and the numbers equal to the pivot after the smaller group. So we can get the right answer to the same test case like:
[2, 4, 1, 1, 5, 5, 5, 5, 5, 9, 10, 7]
And the returned index is also 6.