Given a sorted array of integers and a target value, find out if a target exists in the array or not in O(log(n)) time using Binary Search Algorithm in C and Java. If target exists in the array, print index of it.

For example,

**Input: **

arr[] = [2, 3, 5, 7, 9]

target = 7

** Output: **Element found at index 3

**Input: **

arr[] = [1, 4, 5, 8, 9]

target = 2

** Output: **Element not found

A simple solution would be to perform Linear search on the given array. It sequentially checks each element of the array for the target value until a match is found or until all the elements have been searched. Worst case time complexity of this approach is O(n) as it makes at most n comparisons, where n is the length of the array. This approach don’t take advantage of the fact that array is sorted.

#### How can we perform better?

The idea is to use Binary Search. Binary Search is a divide and conquer algorithm. Like all divide and conquer algorithms, Binary Search first divides a large array into two smaller sub-arrays and then recursively (*or iteratively*) operate the sub-arrays. But instead of operating on both sub-arrays, it discards one sub-array and continue on the second sub-array. This decision of discarding one sub-array is made in just one comparison.

So Binary Search basically reduces the search space to half at each step. By search space we mean sub-array of given array where the target value is located (*if present in the array*). Initially, the search space is the entire array and binary search redefine the search space at every step of the algorithm by using the property of the array that it is sorted. It does so by comparing the mid value in the search space to the target value. If the target value matches the middle element, its position in the array is returned else it discards half of the search space based on the comparison result.

Let us track the search space by using two index – start and end. Initially, start = 0 and end = n-1 (as initially whole array is our search space). At each step, we find the mid value in the search space and compares it with target value. There are three cases possible –

**Case 1:**If target = A[mid],

*we return mid.*

**Case 2:** If target < A[mid],* we discard all elements in the right search space including the mid element i.e. A[mid..high]. Now our new high would be mid – 1.*

**Case 3:** target > A[mid], *we discard all elements in the left search space including the mid element i.e. A[low..mid]. Now our new low would be mid + 1.*

We repeat the process until target is found or our search space is exhausted. Let’s understand this by taking a example. Let

arr = [2, 3, 5, 7, 8, 10, 12, 15, 18, 20]

target = 7

#### 1. Iterative implementation in C and Java –

## C

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#include <stdio.h> // Iterative implementation of Binary Search Algorithm to return // the position of target x in the array A of size N int binarySearch(int A[], int N, int x) { // search space is A[low..high] int low = 0, high = N - 1; // iterate till search space contains at-least one element while (low <= high) { // find the mid value in the search space and // compares it with target value int mid = (low + high)/2; // overflow can happen // int mid = low + (high - low)/2; // int mid = high - (high - low)/2; // target value is found if (x == A[mid]) return mid; // if target is less than the mid element, discard all elements // in the right search space including the mid element else if (x < A[mid]) high = mid - 1; // if target is more than the mid element, discard all elements // in the left search space including the mid element else low = mid + 1; } // target doesn't exist in the array return -1; } // Iterative implementation of Binary Search Algorithm int main(void) { int A[] = { 2, 5, 6, 8, 9, 10 }; int target = 5; int n = sizeof(A)/sizeof(A[0]); int index = ninarySearch(A, n, target); if (index != -1) printf("Element found at index %d", index); else printf("Element not found in the array"); return 0; } |

## Java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
class BinarySearch { // find out if a key x exists in the sorted array A // or not using binary search algorithm public static int binarySearch(int[] A, int x) { // search space is A[left..right] int left = 0, right = A.length - 1; // till search space consists of at-least one element while (left <= right) { // we find the mid value in the search space and // compares it with key value int mid = (left + right) / 2; // overflow can happen. Use: // int mid = left + (right - left) / 2; // int mid = right - (right - left) / 2; // key value is found if (x == A[mid]) { return mid; } // discard all elements in the right search space // including the mid element else if (x < A[mid]) { right = mid - 1; } // discard all elements in the left search space // including the mid element else { left = mid + 1; } } // x doesn't exist in the array return -1; } public static void main(String[] args) { int[] A = { 2, 5, 6, 8, 9, 10 }; int key = 5; int index = binarySearch(A, key); if (index != -1) { System.out.println("Element found at index " + index); } else { System.out.println("Element not found in the array"); } } } |

`Output: `

Element found at index 1

We can easily convert above iterative version of the binary search algorithm to recursive one. Below is the recursive version.

#### 2. Recursive implementation in C and Java –

## C

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <stdio.h> // Recursive implementation of Binary Search Algorithm to return // the position of target x in the sub-array A[low..high] int binarySearch(int A[], int low, int high, int x) { // Base condition (search space is exhausted) if (low > high) return -1; // we find the mid value in the search space and // compares it with target value int mid = (low + high)/2; // overflow can happen // int mid = low + (high - low)/2; // Base condition (target value is found) if (x == A[mid]) return mid; // discard all elements in the right search space // including the mid element else if (x < A[mid]) return binarySearch(A, low, mid - 1, x); // discard all elements in the left search space // including the mid element else return binarySearch(A, mid + 1, high, x); } // Recursive implementation of Binary Search Algorithm int main(void) { int A[] = { 2, 5, 6, 8, 9, 10 }; int target = 5; int n = sizeof(A)/sizeof(A[0]); int low = 0, high = n - 1; int index = binarySearch(A, low, high, target); if (index != -1) printf("Element found at index %d", index); else printf("Element not found in the array"); return 0; } |

## Java

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
class BinarySearch { // Find out if a key x exists in the sorted array // A[left..right] or not using binary search algorithm public static int binarySearch(int[] A, int left, int right, int x) { // Base condition (search space is exhausted) if (left > right) { return -1; } // we find the mid value in the search space and // compares it with key value int mid = (left + right) / 2; // overflow can happen. Use beleft // int mid = left + (right - left) / 2; // Base condition (key value is found) if (x == A[mid]) { return mid; } // discard all elements in the right search space // including the mid element else if (x < A[mid]) { return binarySearch(A, left, mid - 1, x); } // discard all elements in the left search space // including the mid element else { return binarySearch(A, mid + 1, right, x); } } public static void main(String[] args) { int[] A = { 2, 5, 6, 8, 9, 10 }; int key = 5; int left = 0; int right = A.length - 1; int index = binarySearch(A, left, right, key); if (index != -1) { System.out.println("Element found at index " + index); } else { System.out.println("Element not found in the array"); } } } |

`Output: `

Element found at index 1

### Performance of Binary Search Algorithm:

We know that at each step of the algorithm, our search space reduces to half. That means if initially our search space contains n elements, then after one iteration it contains n/2, then n/4 and so on..

n -> n/2 -> n/4 -> … -> 1

Suppose after k steps our search space is exhausted. Then,

n/2^{k} = 1

n = 2^{k}

k = log_{2}n

Therefore, time complexity of binary search algorithm is O(log_{2}n) which is very efficient. Auxiliary space used by it is O(1) for iterative implementation and O(log_{2}n) for recursive implementation due to call stack.

### Avoid Integer Overflow:

*signed int* in C/C++ takes up *4 bytes* of storage i.e. It allows storage for integers between -2147483648 to 2147483647 (*Note that some compilers might take up 2 bytes storage as well. The exact value can be find by cout << sizeof(int)*).

So if (low + high) > 2147483647, integer overflow will happen.

int mid = (low + high)/2; // overflow can happen

To avoid integer overflow, we can use any of the below expressions –

int mid = low + (high – low)/2; OR

int mid = high – (high – low)/2;

Now, *low + (high – low) / 2* or *high – (high – low) / 2* always computes a valid index halfway between high and low, and overflow will never happen even for extreme values.

**Suggested Read: **

1. Binary Search in C++ Standard Library (STL) and Java Collections

2. Binary search vs Ternary Search

**Thanks for reading.**

Please use our online compiler to post code in comments. To contribute, get in touch with us.

Like us? Please spread the word and help us grow. Happy coding 🙂

## Leave a Reply

> Auxiliary space used by it is O(1).

This is technically false, you use recursion so you need space for the stack frames, so the auxiliary space you are using is equal to max recursion depth. If splitting in 2, this means same as time complexity – O(log_2 n)

Thanks Aleksander for bringing this to our notice. We have updated the post.

Happy coding 🙂

the second picture is false? The mid should be A[2]?