Skip to content

[QuickSort] clean the left param +1/-1, beautify judgment conditions#9

Open
PhzCode wants to merge 1 commit intojonhoo:masterfrom
PhzCode:master
Open

[QuickSort] clean the left param +1/-1, beautify judgment conditions#9
PhzCode wants to merge 1 commit intojonhoo:masterfrom
PhzCode:master

Conversation

@PhzCode
Copy link

@PhzCode PhzCode commented Jan 4, 2022

Simplify the while judgment conditions and remove the unused +1/-1 for the left param

// already on the correct side
left += 1;
} else if &rest[right] > pivot {
} else if &rest[right] > pivot && right > 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this conditional change is necessary? After all, we won't get here with the new right > 0 in the while conditional.

let mut left = 0;
let mut right = rest.len() - 1;
while left <= right {
// if right <= 0, it's done
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also point out here (this helped me) that if right <= 0, then left == right, so there can't be a need to swap.

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.

2 participants