-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
156 lines (134 loc) · 3.52 KB
/
binary_search.cpp
File metadata and controls
156 lines (134 loc) · 3.52 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
// C++ template version of binary search ,Not complete
#include <stdio.h>
#include <iostream>
#include <stdio.h>
#include <algorithm> // std::fill
#include <string>
#include <iterator>
#include <array>
#include <cassert>
using namespace std;
template <typename T>
int int_ascending_order (const T& c1, const T& c2){
if (c1 == c2 ) return 0;
if (c1 < c2 ) return 1;
return -1;
}
template <typename T>
class binarysearch {
public:
binarysearch(vector<T>& d,int s,int(*cf) (const T& c1, const T& c2),bool dis ): _darray(d){
//_darray =d;
_size =s;
_cf = cf;
_display = dis;
}
~binarysearch(){}
void _assertSorted();
bool binary_search(const T& tofind, int& pos, int& num_compare); //Called from main !
private:
/* private data */
vector<T>& _darray;
int _size;
int(*_cf) (const T& c1, const T& c2);
bool _display;
bool _binary_search(const T& tofind, int s, int e, int& pos, int& num_compare);
void _assertSorted(int s, int e);
bool _bs();
};
template <typename T>
bool binarysearch<T>::_binary_search(
const T& tofind,
int first,
int last,
int& pos,
int& num_compare)
{
//cout <<"first " << first << " last " << last << endl;
cout << "tofind: " << tofind << " "<<endl;
_assertSorted(first, last); //Array must be in ascending or descending order
//WRITE YOUR CODE To DO BINARY SEARCH HERE>>
//return (_bs())
pos = -1;
while(first <= last){
int mid = first + (last-first)/2;
// cout <<"mid " << mid << endl;
if (_darray[mid] == tofind){
num_compare++;
pos = mid;
// cout << "Found : " << tofind << endl;
return true;
}
if (_darray[mid] < tofind ){
num_compare++;
first = mid+1;
}
else{
last = mid-1;
}
}
return false;
}
template <typename T>
void binarysearch<T>::_assertSorted(int s, int e){
int p = 0;
for (int i = s; i <= e; ++i) {
//cout << _darray[p] << " " << _darray[i] ;
int r = _cf(_darray[p], _darray[i]);
//cout << " " << r <<endl;
assert(r >= 0);
p = i;
}
}
template <typename T>
void binarysearch<T>::_assertSorted(){
int s = _size;
_assertSorted(0, s - 1);
}
template <typename T>
bool binarysearch<T>::binary_search(const T& tofind, int& pos, int& num_compare){
int n = _size;
bool found = _binary_search(tofind, 0, n-1, pos, num_compare);
cout <<"Number of comparisons "<< num_compare <<endl;
if (found ){
cout << "Number "<< tofind << "found " <<endl;
}
else
cout << "Number "<< tofind << " Not found " <<endl;
return found;
}
static void int_test()
{
bool verbose = false;
int a[] = { -5, -3, -1, 0, 1, 4, 7, 7, 9, 10, 21, 45 };
int sz = sizeof(a) / sizeof(int);
vector<int> b ;
b.assign(a,a+sz);
cout << "size " << sz <<endl;
int n = b.size();
cout << "vector size " << n <<endl;
cout << "The last element is: " << b[n - 1]<<endl;
// ={ -5, -3, -1, 0, 1, 4, 7, 7, 9, 10, 21, 45 };
//binarysearch<int>::fill(b, a, sz);
binarysearch<int> bb(b, sz, int_ascending_order, verbose);
int pos = -1;
int nc = 0;
bb.binary_search(45, pos, nc);
assert(pos == 11);
bb.binary_search(4, pos, nc);
assert(pos == 5);
bb.binary_search(-5, pos, nc);
assert(pos == 0);
bb.binary_search(-4, pos, nc);
assert(pos == -1);
bb.binary_search(46, pos, nc);
assert(pos == -1);
bb.binary_search(-6, pos, nc);
assert(pos == -1);
bb.binary_search(44, pos, nc);
assert(pos == -1);
}
int main (){
int_test();
cout << "All test PASSED !!!" <<endl;
}