The lines:
while (a[--j] > pivot) {
if (j == left) {
break;
}
}
Expect the two values of j
to be the same. If you change it to
while (a[j--] > pivot) {
if (j == left) {
break;
}
}
The first referenced j, a[j--]
will be one higher then the next j, j == left
If you wanted to make the change, you would have to change it to:
while (a[j--] > pivot) {
if (j+1 == left) { //adding one back in
break;
}
}
My personal opinion is that the code was cleaner before, since starting one outside a range is common and understandable. Adding one back is is 'fiddly' and will make everyone question whether it is correct.
The clearest way to make your change might be:
while (a[j] > pivot) {
if (j == left) {
break;
}
j--; // waiting until the end to decrement
}
I would consider that pretty clean, and preferable to the original because you start with int j = right
.