-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarysearch.cpp
58 lines (56 loc) · 1.41 KB
/
binarysearch.cpp
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
58
int bs1(int* a, int x, int l, int r) // binary search, find the rightmost element that is less than or equal to x
{ // suppose the array is non-decreasing
while(l < r-1)
{
int mid = (l+r)/2;
if(a[mid] <= x) // if want the leftmost one when there are multiple values equal to x, remove the =
{ // if the array is non-increasing, change < to >
l = mid;
}
else
{
r = mid;
}
}
if(a[r] <= x) // if the array is non-increasing, change < to >
return r; // if want the leftmost one when there are multiple values equal to x, need to first check if(a[l] == x) return l
return l;
}
int bs2(int* a, int x, int l, int r) // binary search, find the rightmost element that is less than or equal to x. if multiple x exists in the array, return the leftmost one.
{ // suppose the array is non-decreasing
while(l < r-1)
{
int mid = (l+r)/2;
if(a[mid] < x)
{ // if the array is non-increasing, change < to >
l = mid;
}
else
{
r = mid;
}
}
if(a[r] < x) // if the array is non-increasing, change < to >
return r;
if(a[r] == x && a[l] != x)
return r;
return l;
}
int bs3(int* a, int x, int l, int r) // binary search, find the rightmost element that is strictly less than x
{ // suppose the array is non-decreasing
while(l < r-1)
{
int mid = (l+r)/2;
if(a[mid] < x)
{
l = mid;
}
else
{
r = mid;
}
}
if(a[r] < x)
return r;
return l;
}