Given a sorted array arr[] of n elements, write a function to search a given element x in arr[].

A simple approach is to do linear search.The time complexity of above algorithm is O(n). Another approach to perform the same task is using Binary Search.

Binary Search: Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.

Example:

Binary Search

c and c++

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20A%20recursive%20binary%20search%20function.%20It%20returns%20location%20of%20x%20in%0A%2F%2F%20given%20array%20arr%5Bl..r%5D%20is%20present%2C%20otherwise%20-1%0Aint%20binarySearch(int%20arr%5B%5D%2C%20int%20l%2C%20int%20r%2C%20int%20x)%0A%7B%0A%20%20%20if%20(r%20%3E%3D%20l)%0A%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20mid%20%3D%20l%20%2B%20(r%20-%20l)%2F2%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20the%20element%20is%20present%20at%20the%20middle%20itself%0A%20%20%20%20%20%20%20%20if%20(arr%5Bmid%5D%20%3D%3D%20x)%20%20return%20mid%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20element%20is%20smaller%20than%20mid%2C%20then%20it%20can%20only%20be%20present%0A%20%20%20%20%20%20%20%20%2F%2F%20in%20left%20subarray%0A%20%20%20%20%20%20%20%20if%20(arr%5Bmid%5D%20%3E%20x)%20return%20binarySearch(arr%2C%20l%2C%20mid-1%2C%20x)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Else%20the%20element%20can%20only%20be%20present%20in%20right%20subarray%0A%20%20%20%20%20%20%20%20return%20binarySearch(arr%2C%20mid%2B1%2C%20r%2C%20x)%3B%0A%20%20%20%7D%0A%20%0A%20%20%20%2F%2F%20We%20reach%20here%20when%20element%20is%20not%20present%20in%20array%0A%20%20%20return%20-1%3B%0A%7D%0A%20%0Aint%20main(void)%0A%7B%0A%20%20%20int%20arr%5B%5D%20%3D%20%7B2%2C%203%2C%204%2C%2010%2C%2040%7D%3B%0A%20%20%20int%20n%20%3D%20sizeof(arr)%2F%20sizeof(arr%5B0%5D)%3B%0A%20%20%20int%20x%20%3D%2010%3B%0A%20%20%20int%20result%20%3D%20binarySearch(arr%2C%200%2C%20n-1%2C%20x)%3B%0A%20%20%20(result%20%3D%3D%20-1)%3F%20printf(%22Element%20is%20not%20present%20in%20array%22)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3A%20printf(%22Element%20is%20present%20at%20index%20%25d%22%2C%20result)%3B%0A%20%20%20return%200%3B%0A%7D” message=”c and c++” highlight=”” provider=”manual”/]

Output

Element is present at index 3

[ad type=”banner”]

Python

[pastacode lang=”python” manual=”%23%20Iterative%20Binary%20Search%20Function%0A%23%20It%20returns%20location%20of%20x%20in%20given%20array%20arr%20if%20present%2C%0A%23%20else%20returns%20-1%0Adef%20binarySearch(arr%2C%20l%2C%20r%2C%20x)%3A%0A%20%0A%20%20%20%20while%20l%20%3C%3D%20r%3A%0A%20%0A%20%20%20%20%20%20%20%20mid%20%3D%20l%20%2B%20(r%20-%20l)%2F2%3B%0A%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Check%20if%20x%20is%20present%20at%20mid%0A%20%20%20%20%20%20%20%20if%20arr%5Bmid%5D%20%3D%3D%20x%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20mid%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20x%20is%20greater%2C%20ignore%20left%20half%0A%20%20%20%20%20%20%20%20elif%20arr%5Bmid%5D%20%3C%20x%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20l%20%3D%20mid%20%2B%201%0A%20%0A%20%20%20%20%20%20%20%20%23%20If%20x%20is%20smaller%2C%20ignore%20right%20half%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20r%20%3D%20mid%20-%201%0A%20%20%20%20%20%0A%20%20%20%20%23%20If%20we%20reach%20here%2C%20then%20the%20element%20was%20not%20present%0A%20%20%20%20return%20-1%0A%20%0A%20%0A%23%20Test%20array%0Aarr%20%3D%20%5B%202%2C%203%2C%204%2C%2010%2C%2040%20%5D%0Ax%20%3D%2010%0A%20%0A%23%20Function%20call%0Aresult%20%3D%20binarySearch(arr%2C%200%2C%20len(arr)-1%2C%20x)%0A%20%0Aif%20result%20!%3D%20-1%3A%0A%20%20%20%20print%20%22Element%20is%20present%20at%20index%20%25d%22%20%25%20result%0Aelse%3A%0A%20%20%20%20print%20%22Element%20is%20not%20present%20in%20array%22%0A” message=”python” highlight=”” provider=”manual”/]

Output

Element is present at index 3

[ad type=”banner”]

Java

[pastacode lang=”java” manual=”%2F%2F%20Java%20implementation%20of%20iterative%20Binary%20Search%0Aclass%20BinarySearch%0A%7B%0A%20%20%20%20%2F%2F%20Returns%20index%20of%20x%20if%20it%20is%20present%20in%20arr%5B%5D%2C%20else%0A%20%20%20%20%2F%2F%20return%20-1%0A%20%20%20%20int%20binarySearch(int%20arr%5B%5D%2C%20int%20x)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20l%20%3D%200%2C%20r%20%3D%20arr.length%20-%201%3B%0A%20%20%20%20%20%20%20%20while%20(l%20%3C%3D%20r)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20m%20%3D%20l%20%2B%20(r-l)%2F2%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Check%20if%20x%20is%20present%20at%20mid%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bm%5D%20%3D%3D%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20m%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20greater%2C%20ignore%20left%20half%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bm%5D%20%3C%20x)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20l%20%3D%20m%20%2B%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20x%20is%20smaller%2C%20ignore%20right%20half%0A%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20r%20%3D%20m%20-%201%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20if%20we%20reach%20here%2C%20then%20element%20was%20not%20present%0A%20%20%20%20%20%20%20%20return%20-1%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%20to%20test%20above%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20BinarySearch%20ob%20%3D%20new%20BinarySearch()%3B%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B2%2C%203%2C%204%2C%2010%2C%2040%7D%3B%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%0A%20%20%20%20%20%20%20%20int%20x%20%3D%2010%3B%0A%20%20%20%20%20%20%20%20int%20result%20%3D%20ob.binarySearch(arr%2C%20x)%3B%0A%20%20%20%20%20%20%20%20if%20(result%20%3D%3D%20-1)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Element%20not%20present%22)%3B%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Element%20found%20at%20index%20%22%2Bresult)%3B%0A%20%20%20%20%7D%0A%7D” message=”java” highlight=”” provider=”manual”/]

Output

Element is present at index 3

[ad type=”banner”]

Time Complexity:

The time complexity of Binary Search can be written as

T(n) = T(n/2) + c

The above recurrence can be solved either using Recurrence T ree method or Master method. It falls in case II of Master Method and solution of the recurrence is \Theta(Logn).

Auxiliary Space: O(1) in case of iterative implementation. In case of recursive implementation, O(Logn) recursion call stack space.

Algorithmic Paradigm: Divide and Conquer.

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,