-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinSearch.cpp
More file actions
168 lines (131 loc) · 3.64 KB
/
BinSearch.cpp
File metadata and controls
168 lines (131 loc) · 3.64 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
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
#include<iostream>
#include<cstring>
#include<string>
#include<time.h>
#include<vector>
#include<algorithm>
using namespace std;
//Version A : find value in range [lo,hi)
int BinSearchA(const vector<int> &orderedArray, const int &value, int lo, int hi)
{
if(lo < 0 || hi > orderedArray.size())
return -1;
while(lo < hi)
{
int mid = (lo + hi) >> 1;
if(value < orderedArray[mid]) hi = mid;
else if(orderedArray[mid] < value) lo = mid + 1;
else return mid;
}
return -1;
}
//Recursion Fib
int RecursionFib(int n)
{
if(n == 0) return 0;
else if(n == 1) return 1;
else return RecursionFib(n-1) + RecursionFib(n-2);
}
//Reverse Fib
int ReverseFib(int fibNum)
{
for(int i = 0; i < 20; i++)
{
if(fibNum == RecursionFib(i) - 1)
{
return RecursionFib(i - 1) - 1;
}
}
return -1;
}
//Version Fib :
int FibBinSearch(const vector<int> &orderedArray, const int &value, int lo, int hi)
{
if(lo < 0 || hi > orderedArray.size())
return -1;
while(lo < hi)
{
int breakPoint = ReverseFib(hi - lo);
if(breakPoint == -1)
{
cout << "Format Error" << endl;
}
if(value < orderedArray[breakPoint]) hi = breakPoint;
else if(orderedArray[breakPoint] < value) lo = breakPoint + 1;
else return breakPoint;
}
return -1;
}
//Version B
int BinSearchB(const vector<int> &orderedArray, const int &value, int lo, int hi)
{
if(lo < 0 || hi > orderedArray.size())
return -1;
//有效查找区间缩短至1时,才会终止算法
while(hi - lo > 1)
{
int mid = (lo + hi) >> 1;
value < orderedArray[mid] ? hi = mid : lo = mid;
}
//出口时hi = lo + 1,查找区间只剩一个元素orderedArray[lo]
//返回命中元素的秩,或者返回-1
return orderedArray[lo] == value ? lo : -1;
}
//Version C
//该版本严格遵守语义:返回不大于value的最后一个元素
int BinSearchC(const vector<int> &orderedArray, const int &value, int lo, int hi)
{
if(lo < 0 || hi > orderedArray.size())
return -1;
//不变性:orderedArray[0, lo) <= value < orderedArray[hi, n)
while(lo < hi)
{
int mid = (lo + hi) >> 1;
value < orderedArray[mid] ? hi = mid : lo = mid + 1;
}
//出口时,orderedArray[lo = hi]为大于value的最小元素
//故lo-1为不大于value的元素的最大秩
return --lo;
}
//input check ordered
bool OrderedSequenceCheck(vector<int> &orderedArray)
{
string arg;
cin >> arg;
while(arg != "end")
{
int value = atoi(arg.c_str());
orderedArray.push_back(value);
cin >> arg;
}
for(int i = 0; i < orderedArray.size(); i++)
{
cout << orderedArray[i]<<endl;
}
return is_sorted(orderedArray.begin(),orderedArray.end()) ? true : false;
}
int main(int argc, char const *argv[])
{
vector<int> orderedArray;
if(!OrderedSequenceCheck(orderedArray))
{
cout << "this sequence is not ascending sequence" << endl;
return 0;
}
else
{
cout << "this sequence is ascending sequence" << endl;
}
int lo = 0;
int hi = orderedArray.size();
int resultIndex = -1;
int value = -1;
cout << "please input the value to search" << endl;
cin >> value;
//resultIndex = BinSearchA(orderedArray, value, lo, hi);
//resultIndex = FibBinSearch(orderedArray, value, lo, hi);
//resultIndex = BinSearchB(orderedArray, value, lo, hi);
resultIndex = BinSearchC(orderedArray, value, lo, hi);
cout << resultIndex << endl;
return 0;
}