-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss_listy.cpp
61 lines (52 loc) · 902 Bytes
/
ss_listy.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
#include <iostream>
#include <math.h>
#define NULL_VALUE -1
#define SIZE 100
using namespace std;
class Listy{
public:
Listy();
~Listy();
int elementAt(int);
int l[SIZE];
};
Listy::Listy(){
for(int i=0; i<SIZE; i++){
l[i] = 2*i;
}
}
int Listy::elementAt(int i){
if(i < SIZE){
return l[i];
}
return NULL_VALUE;
}
int bsListy(Listy* listy,int x){
int start = 0;
int end = 0;
int c = 0;
int middle = 0;
while(listy->elementAt(end) != -1 && listy->elementAt(end) <= x){
start = end;
end = pow(2,c) - 1;
c += 1;
}
while(start <= end){
middle = (start + end)/2;
if(listy->elementAt(middle) == -1 || listy->elementAt(middle) > x){
end = middle - 1;
}
else if(listy->elementAt(middle) < x){
start = middle + 1;
}
else{
return middle;
}
}
return NULL_VALUE;
}
int main(){
Listy* listy = new Listy();
cout << bsListy(listy, 20) << endl;
return 0;
}