You are on page 1of 4

Binary Search

Binary Search is a search algorithm that is used to find the position of an element (target value ) in a sorted
array. The array should be sorted prior to applying a binary search.

The binary search algorithm works by comparing the element to be searched by the middle element of the array
and based on this comparison follows the required procedure.
Case 1 − element = middle, the element is found return the index.
Case 2 − element > middle, search for the element in the sub-array starting from middle+1 index to n.
Case 3 − element < middle, search for element in the sub-array starting from 0 index to middle -1.

Algorithm
Step 1 : Find the middle element of array. using ,
middle = initial_value + end_value / 2 ;
Step 2 : If middle = element, return ‘element found’ and index.
Step 3 : if middle > element, call the function with end_value = middle - 1 .
Step 4 : if middle < element, call the function with start_value = middle + 1 .

Implementation: Iteration
#include <stdio.h>
/*This function takes the following parameter:
- the array that we are searching through
- the starting index of the array
- the ending index of the array
- the element we are searching for
The function returns the index where the element was found*/
int binarySearch(int array[], int start_index, int end_index, int element){
while (start_index <= end_index){//while there are elements in the array
int middle = start_index + (end_index - start_index )/2;
//find middle element
if (array[middle] == element) //CASE 1: the element we are looking for is
equal to the middle element

return middle; // return the index of the middle value as the location
if (array[middle] < element)//CASE 2: the middle element is less than the
element we are searching for

start_index = middle + 1; //adjust the start index to be 1 greater than


middle – effectively discarding half the array that contains values smaller than the one
we are searching for

else //CASE 3: the middle element is greater than the element we are searching for
end_index = middle - 1; //adjust the end index to be 1 less than middle –
effectively discarding half the array that contains values larger than the one we are
searching for

}
return -1; //CASE 4: Element not in the array:return -1 if we don’t find the element
}
int main(void){
int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7;
int element = 16;
int found_index = binarySearch(array, 0, n-1, element);
if(found_index == -1 ) {
printf("Element not found in the array ");
}
else {
printf("Element found at index : %d",found_index);
}
return 0;
}

How the implementation works


Suppose we have the following array [Note that it is sorted]:

array 1 4 7 9 16 56 70
0 1 2 3 4 5 6

Suppose the element we are searching for is 16


The parameters to the function binarySearch() will be:
• int array[] : array
• int start_index: 0
• int end_index: 6
• int element: 16

When binarySearch(array, 0, n-1, element) is called in the main function above, the following
happens:
start is LESS THAN OR EQUAL to end, so we enter the loop and :
Calculate middle index: 0 + (6 - 0)/2 = 0+6/2 = 0+3 = 3

array 1 4 7 9 16 56 70
0 1 2 3 4 5 6
start end
Comparison 1: CASE 1: 9 == 16? CASE 2: 9<16? CASE 3: 9>16?
• CASE 2 is true so we adjust the start index to middle + 1 [3+1=4]

array 1 4 7 9 16 56 70 array 16 56 70
0 1 2 3 4 5 6 4 5 6
à start end start end

start is LESS THAN OR EQUAL to end, so we enter the loop again:

Calculate middle index: ( 4 + (6 - 4) )/2 = 4+2/2 = 4+1 = 5

array 16 56 70
4 5 6
start end

Comparison 2: CASE 1: 56 == 16? CASE 2: 56<16? CASE 3: 56>16?


• CASE 3 is true so we adjust the end index to middle – 1 [5-1=4]

array 16 56 70 array 16
4 5 6 4
start start
end ß end

start is LESS THAN OR EQUAL to end, so we enter the loop again:

Calculate middle index: ( 4 + (4 - 4) )/2 = 4+0/2 = 4+0 = 4


array 16
4
start
end

Comparison 3: CASE 1: 16 == 16? CASE 2: 16<16? CASE 3: 16>16?


At this point, case 1 is true and the return middle; effectively exits the function.

Analysis of the algorithm


Notice that this algorithm constantly divides the array in half until middle value matches the element being
searched for.
Any return statement effectively terminates a function. No statement after return is ever evaluated.

In evaluating the example above, 3 comparisons are made. Using a linear search, it would have taken 5
comparisons because the value is located at index 4. 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. We say the that time complexity or worst-
case scenario of binary search is O(log n). This is an improvement on the time complexity of linear search
which is O(n). Binary search is especially more efficient on larger input sets. To understand the
improvement in time efficiency, consider the following graph for a linear and logarithmic function:

You might also like