Re:Mathematics for Computer Science [pdf]
by garethrees on Tuesday March 14, 2017 @06:37PM
(#13872131)
(Slightly off topic, but maybe still interesting.) I used to write binary searches like yours, with two tests per iteration:
But then I discovered that there's an alternative approach which has only one test per iteration:
If comparisons are expensive compared to other operations then this is twice as fast as the variant with two comparisons per iteration. (If comparisons are cheap, as they are if you are searching an array of numbers in a compiled language, then it doesn't make much difference.)
[ Parent
| on HN ]
There are no comments on this comment yet.