In order to choose the input between the left or right sequences, you need to know if the sequence is exhausted. You check this by testing the pointer to see if it's beyond the valid range. left < lend
returns true
if the left sequence is not exhausted, and right >= rend
returns true
if the right sequence is exhausted. Thus the if
will be taken when the left sequence isn't exhausted, and either the left item is less than the right item or the right sequence is exhausted.
If you were following the standard library iterator conventions, you would never check for <
or >
in a comparison. left != lend
and right == rend
would work just as well in the code you posted.
P.S. two thoughts that aren't part of your question:
- As noted in the comments, there's undefined behavior lurking in the comparison. You need to test if the right side is exhausted before checking the value at the right side pointer.
- Merge sort is naturally stable if you use the correct comparison. You want any ties to take from the left sequence. In your case of sorting
int
s it doesn't matter since you can't tell one identicalint
from another, but if this was just a key in part of a larger structure it could matter greatly. It's just a matter of replacing<
with<=
.
Taking all the above suggestions together you end up with:
if (left != lend && (right == rend || *left <= *right))