Binary Search

The goal for this exercise is to familiarize yourself with the binary search algorithm by implementing it in code.

What you need to do to prepare for this exercise:


The pseudocode for a binary search is:

Given a target value, an array, and it's size

Initialize max to array.Length-1 Initialize min to 0 While min <= max Figure out the index (number) of the slot that's half way between min and max (let’s call this variable half) If the value at array[half] is what we're looking for Return "true" to the caller to indicate that we've found the value Else if the value at array[half] is larger than the target Clearly, the target is between min and half (or not present) So assign half-1 to max, and go back to the top of the loop Else ( the value at array[half] is smaller than the target) Clearly, the target is between half and max (or not present) So assign half+1 to min, and go back to the top of the loop. End If End of While loop // The only way we could make it to this part of the function is if we never found the target value. Return "false" to the caller to indicate that we didn't find the value

Once you’ve done that, you should test the function (once you've written it), by adding code to Main().

What you need to do for this exercise
  1. Implement the FindIntegerBinary method, within the SearchingAndSorting class, using the algorithm described above.