blob: 2c8a0e98e32136492f87000d4be7f9600bce3930 (
plain) (
blame)
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
|
/*
A iterative binary search function.
It returns location of x in given array arr[l..r] if present,
otherwise -1.
*/
//int l - left side index
//int r - right side index
//int m - middle index
//int x - number 'x' we are searching for
int binarySearch1(const int *arr, int l, int r, int x)
{
while (l <= r)
{
int m = l + (r - l) / 2;
if (arr[m] == x)// Check if x is present at mid
return m;
if (arr[m] < x)// If x greater, ignore left half
l = m + 1;
else
r = m - 1; // If x is smaller, ignore right half
}
return -1; // if we reach here, then the element was not present
}
|