This example shows how Binary Search Algorithm works.

Binary search algorithm is better when an array is sorted because it makes comparison between the search key “k” and middle element of the array. Since the array is sorted, the comparison results either in a match between “k” and the middle element of the array or identifying the left half or right half of the array to which the desired element may belong. This process continues on the half in which the desired element may be present in case “k” is not equal to the middle element. In this way, either the element is detected or the final division leads to a half, where the array does not contain the desired element “k”.

This is very efficient method of searching algorithm because the comparison enables one to eliminate half of the elements from further consideration.

The complete source code

Output

Thanks for reading.

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.