-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathbinary_search.cpp
222 lines (193 loc) · 4.87 KB
/
binary_search.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* @Author: Chacha
* @Date: 2018-11-24 19:05:12
* @Last Modified by: Chacha
* @Last Modified time: 2021-03-26 18:05:40
*/
/**
* The steps of binary search:
* 1. Let min = 0 , and let max = n where n is the highest possible index value;
* 2. Find the average of min and max, round down so it’s an integer. This is our guess;
* 3. If we guessed the number, stop, we got it;
* 4. If guess is too low, set min equal to one more than guess;
* 5. If guess is too high, set max equal to one less than guess;
* 6. Go back to step two.
*
* 3 Parts of a Successful Binary Search
* 1. Pre-processing - Sort if collection is unsorted.
* 2. Binary Search - Using a loop or recursion to divide search space in half after each comparison.
* 3. Post-processing - Determine viable candidates in the remaining space.
*/
#include <iostream>
#include <vector>
using namespace std;
// Binary search template
int binarySearch(vector<int> &nums, int target)
{
if (nums.empty())
{
return -1;
}
int mid;
int min = 0;
int max = nums.size() - 1;
while (min + 1 < max)
{
// Prevent (left + right) overflow
mid = min + (max - min) / 2;
if (nums[mid] == target)
{
return mid;
}
else if (nums[mid] < target)
{
min = mid;
}
else
{
max = mid;
}
}
if (nums[min] == target)
{
return min;
}
if (nums[max] == target)
{
return max;
}
return -1;
}
/***********************************************************************************
* Given a sorted array of integers, find the starting and ending position of a given target value.
* Your algorithm's runtime complexity must be in the order of O(log n).
* If the target is not found in the array, return [-1, -1].
* For example, Given [5, 7, 7, 8, 8, 10] and target value 8, return [3, 4].
************************************************************************************/
vector<int> binarySearchForRange(vector<int> nums, int target)
{
int start, end, mid;
vector<int> bound(2);
// Search for left bound
start = 0;
end = nums.size() - 1;
while (start + 1 < end)
{
// Prevent (left + right) overflow
mid = start + (end - start) / 2;
if (nums[mid] == target)
{
end = mid;
}
else if (nums[mid] < target)
{
start = mid;
}
else
{
end = mid;
}
}
if (nums[start] == target)
{
bound[0] = start;
}
else if (nums[end] == target)
{
bound[0] = end;
}
else
{
bound[0] = bound[1] = -1;
return bound;
}
// Search for right bound
while (start + 1 < end)
{
// Prevent (left + right) overflow
mid = start + (end - start) / 2;
if (nums[mid] == target)
{
start = mid;
}
else if (nums[mid] < target)
{
start = mid;
}
else
{
end = mid;
}
}
if (nums[end] == target)
{
bound[1] = end;
}
else if (nums[start] == target)
{
bound[1] = start;
}
else
{
bound[0] = bound[1] = -1;
return bound;
}
return bound;
}
/***********************************************************************************
* Given a sorted array and a target value, return the index if the target is found.
* If not, return the index where it would be if it were inserted in-order.
* You may assume NO duplicates in the array.
* Source: https://leetcode.com/problems/search-insert-position/
*
* Example:
* [1,3,5,6], 5 → 2
* [1,3,5,6], 2 → 1
* [1,3,5,6], 7 → 4
* [1,3,5,6], 0 → 0
************************************************************************************/
int searchInsert(vector<int> nums, int target)
{
if (nums.empty())
{
return -1;
}
int start = -1, mid, end = nums.size();
while (start + 1 < end)
{
mid = start + (end - start) / 2;
if (nums[mid] == target)
{
return mid;
}
else if (nums[mid] < target)
{
start = mid;
}
else
{
end = mid;
}
}
return start + 1;
}
// Print vector value
void printVector(vector<int> &vec)
{
for (int i = 0; i < vec.size(); i++)
{
printf("Vector is %3d", vec[i]);
printf("\n");
}
cout << endl;
}
int main()
{
int arr[] = {1, 3, 4, 6, 7, 8, 10, 10, 10, 13, 14, 18, 19, 21, 24, 37, 40, 45, 71};
vector<int> nums(arr, arr + sizeof(arr) / sizeof(int));
int target = 10;
int result1 = binarySearch(nums, target);
vector<int> result2 = binarySearchForRange(nums, target);
cout << "Binary search result is " << result1 << "\n";
printVector(result2);
}