# Binary Search Algorithm | Interview Cake

A binary search algorithm finds an item in a sorted array in time.

A animal pull search would walk through the solid range, taking clock in the worst subject .
Let ‘s say we have a sorted range of numbers. To find a number with a binary search, we :

1. Start with the middle number: is it bigger or smaller than our target number? Since the array

is sorted, this tells us if the target would be in the left half or the right half of our range.

2. We’ve effectively divided the problem in half. We can “rule out” the whole half of the align that we know doesn’t contain the target number.
3. Repeat the same approach (of starting in the middle) on the new half-size problem. Then do it again and again, until we either find the number or “rule out” the whole set.

We can do this recursively, or iteratively. here ‘s an iterative version :

public static boolean binarySearch ( int target, int [ ] nums ) { // see if aim appears in nums // we think of floorIndex and ceilingIndex as “ walls ” around // the possible positions of our target, thus by -1 below we mean // to start our wall “ to the leave ” of the 0th index // ( we * do n’t * mean “ the last index ” ) int floorIndex = -1 ; int ceilingIndex = nums.length ; // if there is n’t at least 1 exponent between floor and ceiling, // we ‘ve run out of guesses and the number must not be deliver while ( floorIndex + 1 < ceilingIndex ) { // find the index ~halfway between the deck and ceiling // we use integer division, so we 'll never get a  half index '' int distance = ceilingIndex - floorIndex ; int halfDistance = distance / 2 ; int guessIndex = floorIndex + halfDistance ; int guessValue = nums [ guessIndex ] ; if ( guessValue == aim ) { retort genuine ; } if ( guessValue > target ) { // target is to the impart, then move ceiling to the bequeath ceilingIndex = guessIndex ; } else { // target is to the right, so act floor to the correct floorIndex = guessIndex ; } } return false ; }
How did we know the time cost of binary search was ? The only non-constant function of our time cost is the total of times our while loop runs. Each footstep of our while loop cuts the compass ( dictated by floorIndex and ceilingIndex ) in half, until our range has good one element left .
So the question is, “how many times must we divide our original array size (n) in half until we get down to 1?”
nitrogen * \frac { 1 } { 2 } * \frac { 1 } { 2 } * \frac { 1 } { 2 } * \frac { 1 } { 2 } * … = 1
How many \frac { 1 } { 2 } ‘s are there ? We do n’t know yet, but we can call that number ten :

north * ( \frac { 1 } { 2 } ) ^x = 1
now we solve for ten :
north * \frac { 1^x } { 2^x } = 1
n * \frac { 1 } { 2^x } = 1
\frac { normality } { 2^x } = 1
normality = 2^x
now to get the ten out of the exponent. How do we do that ? Logarithms .
Recall that \log_{10}{100} means, “what power must we raise 10 to, to get 100”? The answer is 2.
so in this font, if we take the \log_ { 2 } of both sides …
\log_ { 2 } { nitrogen } = \log_ { 2 } { 2^x }
The right hand slope asks, “ what office must we raise 2 to, to get 2^x ? ” Well, that ‘s just x.

\log_ { 2 } { north } = x
therefore there it is. The number of times we must divide north in one-half to get down to 1 is log_ { 2 } n. so our full prison term monetary value is

Careful: we can only use binary search if the input array is already sorted.