General technique.
Steps: Recursive
Note: Binary array.
Working Clement
5. 4. Findthe 3. Binary
2. 1. Recursive
method
Iterative
method
2. 1.
with element.Ifmiddle
(mid)
element. If(Low) 2
the the 2 (Low) position Set Let, Suppose,
2 search search
in
thevalue value the x Principle a
middle tw o =7 sorted
Otherwise, middle respectively. 3 the
algorithm algorithm
of ofx that nmethod
3 pointers following array.
element isx is position 3 needs
equal Binary
searchin can Algorithm
Binary
Search
greater 4 4
The isa
the such to apply
4 find array searchinpg
clement
Low by value to (mid)
setting: than the as in
low out is two
= middle of 5
the of (mid) 5 High 5 used always
(ochnique
1 mid Low + the using
the and followedcanbe ways
middle array to
+ x of 2 the perform
high such finds
1 isthe binary to
to 6 with 6
element array, into as out find
compare the the from out
return formula
the search binary divide
and as
of lowest the the
array, algorithm. position
with the searching. middle
(High)
middle
the (High) and
it conquer of of
compares value highest
any any
(mid)
of
nareh Algorithm
Binary Search Algorithm
0. le, the value ofx is compared with the midlle
the array by setting: clement on left sicde ol
lligh mid
7. Repeat steps 3to 6 until tlhe low cquals to hip.
4
letl side (with respect to middle element)
(6 8
Right side (with respect to middle element)
8, Hence, N =7 is found.
7
(Searched element)
1. Iterative Method
Algorithm: Binary Search (lterative method)
do until the pointers low and high meet each other.
mid = (low+ high) / 2
if (x=array[mid])
return mid
else if (x > array[mid) llx is on the right side
low = mid + 1
else Ilx is on the left side
high = mid 1
Source Code (Iterative Method)
#include <stdio.h>
int binarySearch(int array [], int x, int low, int high) {
I/ Repeat until the pointers low and high meet each other
while (low< high) {
int mid = low + (high - low) / 2;
if (x array[mid){return mid; }
if (x > array[mid]) {low = mid + 1;}
else {high = mid - 1;}
return -1;
BinarySearch Algorithm
int main(void) {
int arrayl| (2, 3, 4, 5,6, 7, 8}:
int n
int x
sizeoflurray) /sizeol(urray|0D.
7:
int result
i (result)
binaryScurch(urray, x, 0, n- ):
printt"Not lound");
clse
printt"Element is lound at index %d", resul):
return 0;
2. Recursive Method
Algorithm: Binary Seareh (Recursive method)
Binary-search(array, x, low, high)
if low > high
return False
else
mid =(low + high) /2
if x==array[mid]
return mid
else if x > array[mid] Ilx is on the right side
return Binary-search(array, x, mid + 1, high)
else
lx is on the left side
return Binary-search(array, x, low, mid - 1)
Source Code (Recursive Method)
#include <stdio.h>
int binary Search(int array[], int x, int low, int high) {
if (high > low) {
int mid =low + (high - low) /2;
if (x =array[mid]) {return mid;}
if (x > array[mid]) {return binarySearch(array, x, mid + 1, high);}
return binary Search(array, x, low, mid - 1);
return -1;
3
BinarySearch Algorithm
int main(void)
int arrayll=(2, 3, 4, 5, 6, 7, 8):
int n = sizeof(array)/
int x =7; sizeof(array|0|);
int result
=binaryScarch(array, x, 0, n - 1);
if (result =-1)
printf("Not found")%
else
printf("Element is found at index %d", result);
Complexity of Binary Search
" Time Complexities
1. Best case complexity: O(1)
2. Average case complexity: 0(log n)
3. Worst case complexity: O(log n)
Space Complexity: The space complexity of the binary search is O(1).
Applications of Binary Search
While debugging, the binary search is used to pinpoint the place where the error
happens.