Intel Corporation interview question

How to perform a binary search?

Interview Answer

Anonymous

2 June 2015

On a sorted list of objects comparable to your search key, pick the middle object. If the object you picked is equal your key, you're done. If it is greater than your object, discard the whole upper half of the list including the object you picked. If it is less than your object, discard the whole lower half of the list including the object you picked. Then perform the same operation again on the smaller list until you have found your key. The Big O performance complexity is logarithmic, or log2(N), which is pretty good.