You are on page 1of 4

Binary search algorithm

Generally, to find a value in unsorted array, we should look through elements of an


array one by one, until searched value is found. In case of searched value is absent
from array, we go through all elements. In average, complexity of such an algorithm is
proportional to the length of the array.
Situation changes significantly, when array is sorted. If we know it, random access
capability can be utilized very efficiently to find searched value quick. ost of
searching algorithm reduces to binary logarithm of the array length. !or reference,
log"#$ %%% %%%& ' "%. It means, that in worst case, algorithm makes "% steps to find a
value in sorted array of a million elements or to say, that it doesn(t present it the array.
Algorithm
)lgorithm is quite simple. It can be done either recursively or iteratively*
$. get the middle element+
". if the middle element equals to the searched value, the algorithm stops+
,. otherwise, two cases are possible*
o
searched value is less, than the middle element. In this case, go to the step
$ for the part of the array, before middle element.
o
searched value is greater, than the middle element. In this case, go to the
step $ for the part of the array, after middle element.
-ow we should define, when iterations should stop. !irst case is when searched element
is found. Second one is when subarray has no elements. In this case, we can conclude,
that searched value doesn(t present in the array.
Examples
Example 1. !ind . in /0$, 1, ., $2, $3, "1, 4., 52, $%", $$46.
Step $ #middle element is $3 7 .&* 0$ 1 . $2 $3 "1 4. 52 $%" $$4
Step " #middle element is 1 8 .&* 0$ 1 . $2 $3 "1 4. 52 $%" $$4
Step , #middle element is . 99 .&* 0$ 1 . $2 $3 "1 4. 52 $%" $$4
Example 2. !ind $%, in /0$, 1, ., $2, $3, "1, 4., 52, $%", $$46.
Step $ #middle element is $3 8 $%,&* 0$ 1 . $2 $3 "1 4. 52 $%" $$4
Step " #middle element is 52 8 $%,&* 0$ 1 . $2 $3 "1 4. 52 $%" $$4
Step , #middle element is $%" 8 $%,&* 0$ 1 . $2 $3 "1 4. 52 $%" $$4
Step 4 #middle element is $$4 7 $%,&* 0$ 1 . $2 $3 "1 4. 52 $%" $$4
Step 1 #searched value is absent&* 0$ 1 . $2 $3 "1 4. 52 $%" $$4
Complexity analysis
:uge advantage of this algorithm is that it(s complexity depends on the array size
logarithmically in worst case. In practice it means, that algorithm will do at most
log"#n& iterations, which is a very small number even for big arrays. It can be proved
very easily. Indeed, on every step the size of the searched part is reduced by half.
)lgorithm stops, when there are no elements to search in. ;herefore, solving following
inequality in whole numbers*
n < "
iterations
7 %
resulting in
iterations 89 log"#n&.
It means, that binary search algorithm time complexity is =#log"#n&&.
Code snippets.
>ou can see recursive solution for ?ava and iterative for @@ below.
Java
/**
* searches for a value in sorted array
*
* @param array
* array to search in
* @param value
* searched value
* @param left
* index of left boundary
* @param right
* index of right boundary
* @return position of searched value, if it presents in the array or
-1, if
* it is absent
*/
int binarySearch(int[] array, int value, int left, int right)
if (left ! right)
return -1"
int #iddle $ (left % right) / &"
if (array[#iddle] $$ value)
return #iddle"
else if (array[#iddle] ! value)
return binarySearch(array, value, left, #iddle - 1)"
else
return binarySearch(array, value, #iddle % 1,
right)"
'
C++
/*
* searches for a value in sorted array
* arr is an array to search in
* value is searched value
* left is an index of left boundary
* right is an index of right boundary
* returns position of searched value, if it presents in the array
* or -1, if it is absent
*/
int binarySearch(int arr[], int value, int left, int right)
(hile (left )$ right)
int #iddle $ (left % right) / &"
if (arr[#iddle] $$ value)
return #iddle"
else if (arr[#iddle] ! value)
right $ #iddle - 1"
else
left $ #iddle % 1"
'
return -1"
'

You might also like