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 partition function #3

Closed
wants to merge 1 commit into from
Closed

Conversation

Gothor
Copy link

@Gothor Gothor commented Sep 7, 2018

It only fixes the partition function, you still would have to find a way to animate it.

@shiffman
Copy link
Member

shiffman commented Sep 8, 2018

Where's my facepalm emoji. . I am happy to merge this but I am going to play around with it a bit more (after some sleep) and see where I went wrong.

@Gothor
Copy link
Author

Gothor commented Sep 8, 2018

Not sure that's the good place for it (please excuse me if it's not the case), but here is me trying a little explanation.

left <= right doesn't work, because it would cause left to increment when it's equal to right. Making left = right + 1. But it may be array[right] > pivot if there's been a swap before. (But later, you used to swap left and the pivot.)

left < hi - 1 doesn't work if the last value is the only one higher than the pivot. Because it would end with left = right = hi - 1, and still, the last swap would have to occur, since it may be array[hi-1] > pivot. So it seems better to check the values.

Then, if this last swap happens, you indeed should return left.
But if it doesn't, you should return hi as the pivot didn't move.

@duskvirkus duskvirkus closed this Oct 7, 2020
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