-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSearchCycSortedArray.cc
57 lines (48 loc) · 1.32 KB
/
SearchCycSortedArray.cc
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
#include <iostream>
#include <vector>
using namespace std;
int search0(const vector<int>& array)
{
int n = array.size();
int left = 0;
int right = n-1;
if (array[left] < array[right])
return left;
int minId = 0;
while (left < right) {
int mid = left + ((right - left) >> 1);
if (array[left] < array[mid]) {
minId = (array[left] < array[minId]) ? left : minId;
left = mid + 1;
} else {
minId = (array[mid+1] < array[minId]) ? (mid+1) : minId;
right = mid;
}
}
return (array[left] < array[minId]) ? left : minId;
}
//Improved solution
int search(const vector<int>& array)
{
int n = array.size();
int left = 0;
int right = n-1;
while (left < right) {
int mid = left + ((right - left) >> 1);
if (array[mid] > array[right])
left = mid + 1;
else
right = mid;
}
return left;
}
//If there are duplicate elements, then for the case array[mid] ==
//array[right], both half must be searched. Hence recursive solution should be
//used. See EPI Page 136 for details.
int main(int argc, char** argv)
{
vector<int> test = {378, 478, 550, 631, 103, 203, 220, 234, 279, 368};
// vector<int> test = {1};
cout << search(test) << endl;
return 0;
}