Skip to content
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

fix clrs-07-01-02 solution bug #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

fix clrs-07-01-02 solution bug #36

wants to merge 1 commit into from

Conversation

chaoyan1037
Copy link

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.

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.

1 participant