-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpperBound.cpp
More file actions
57 lines (44 loc) · 1.45 KB
/
UpperBound.cpp
File metadata and controls
57 lines (44 loc) · 1.45 KB
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
/// This program will find the Upper Bound from a certain Data Set for a given value
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 13;
int main()
{
/// Demo array
int data[SIZE] = {11, 23, 33, 40, 45, 45, 50, 50, 50, 90, 95, 100, 100};
int begI, midI, endI, value;
begI = 0;
endI = SIZE - 1;
midI = (begI + endI) / 2;
/// Demo value to be searched for its Upper Bound
value = 0;
/// If the searched value lies between 1st and Last element of the list,
/// then, surely begI will be equal to endI - 1, when the desired Upper Bound is found
while (begI != endI - 1) {
/// If value is less than the 1st element,
/// Surely, the Upper Bound of that value is zero (0)
/// Hence midI is set to -1,
/// Since, we are printing midI + 1
if (value < data[0]) {
midI = -1;
break;
}
/// If value is greater than or equal to the Last element,
/// Surely, the Upper Bound of that value is equal to the total number of elements (i.e. SIZE)
/// Hence midI is set to SIZE - 1,
/// Since, we are printing midI + 1
else if (value >= data[SIZE - 1]) {
midI = SIZE - 1;
break;
}
if (data[midI] <= value) {
begI = midI;
}
else {
endI = midI;
}
midI = (begI + endI) / 2;
}
printf("%d\n", midI + 1);
return 0;
}