While it is true the code in split
cannot cause high
to become less than low
, using >=
is useful because:
- The routine may be called with
high
already less than low
. For example, if a program filters a list of some things, and ends up with zero things1 and then tries to sort the empty list using quicksort
, it will call it with low
= 0 and high
= ?1.2 When this happens, we want to split
to exit without making any modifications.
- Using
>=
generally has the same performance as ==
; the processor does not do any extra work for >=
. Commonly, int
values are compared with a single instruction that completely determines the relationship (<
, =
, or >
, sometimes with other results as well) and reports them in flags or a result register. Then another instruction branches based on the results.
- It is good practice to guard against bugs. In case some mistake in the source code causes
high
to become less than low
, we might prefer the routine to exit before doing any further damage. (Which decisions to make in such cases are generally sensitive to the situation.)
Footnote
1 For example, consider a real estate search engine for which a user has requested information about houses for sale in a certain town. The software might retrieve all such houses from a database. Then it might apply the user’s filters, such as eliminating all houses except those that have three bedrooms and are less than a certain price. The result of those filters might be an empty list.
2 In the original code, quicksort
contains its own test that prevents it from calling split
with low
greater than high
, so this reason does not apply with this particular calling code. Nonetheless, it is a good design for split
to behave well when called with low
greater than high
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…