-
Notifications
You must be signed in to change notification settings - Fork 0
/
floorAndCeiling.cpp
94 lines (77 loc) · 2.49 KB
/
floorAndCeiling.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
// Function to find the ceil of `x` in a sorted array nums[0…n-1]
// i.e., the smallest integer greater than or equal to `x`
int getCeil(int nums[], int n, int x)
{
// search space is nums[low…high]
int low = 0, high = n - 1, mid;
// initialize ceil to -1
int ceil = -1;
// loop till the search space is exhausted
while (low <= high)
{
// find the mid-value in the search space
mid = (low + high) / 2;
// if `x` is equal to the middle element, it is the ceil
if (nums[mid] == x) {
return nums[mid];
}
// if `x` is less than the middle element, the ceil exists in the
// subarray nums[low…mid]; update ceil to the middle element
// and reduce our search space to the left subarray nums[low…mid-1]
else if (x < nums[mid])
{
ceil = nums[mid];
high = mid - 1;
}
// if `x` is more than the middle element, the ceil exists in the
// right subarray nums[mid+1…high]
else {
low = mid + 1;
}
}
return ceil;
}
// Function to find the floor of `x` in a sorted array nums[0…n-1],
// i.e., the largest integer less than or equal to `x`
int getFloor(int nums[], int n, int x)
{
int low = 0, high = n - 1, mid;
// initialize floor to -1
int floor = -1;
// loop till the search space is exhausted
while (low <= high)
{
// find the mid-value in the search space
mid = (low + high) / 2;
// if `x` is equal to the middle element, it is the floor
if (nums[mid] == x) {
return nums[mid];
}
// if `x` is less than the middle element, the floor exists in the left
// subarray nums[low…mid-1]
else if (x < nums[mid]) {
high = mid - 1;
}
// if `x` is more than the middle element, the floor exists in the
// subarray nums[mid…high]; update floor to the middle element
// and reduce our search space to the right subarray nums[mid+1…high]
else {
floor = nums[mid];
low = mid + 1;
}
}
return floor;
}
int main(void)
{
int nums[] = { 1, 4, 6, 8, 9 };
int n = sizeof(nums) / sizeof(nums[0]);
for (int i = 0; i <= 10; i++)
{
printf("Number %d —> ", i);
printf("ceil is %d, ", getCeil(nums, n, i));
printf("floor is %d\n", getFloor(nums, n, i));
}
return 0;
}