recently I was asked this question in an interview.
In binary search we have two comparisons one for greater than and other for less than the mid value. Can it be optimized in a way so that we need to check only once ?
recently I was asked this question in an interview. In binary search we have
Share
Yes, in several ways.
By doing a three-way comparison, which exists in some versions Fortran (see “arithmetic IF”).
By playing tricks with the wording – in x86 (and others) assembly you can use one
cmpbut still use two branches.By using the Deferred Detection of Equality trick.