forked from EDAII/Lista1-AndreLucas-LeonardoMedeiros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
210 lines (181 loc) · 4.87 KB
/
main.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
#include <iostream>
#include <time.h>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define NOT_FOUND -404
#define BINARY_SEARCH 0
#define SEQUENTIAL_SEARCH 1
#define SENTINEL_SEQUENTIAL_SEARCH 2
#define INDEXED_SEQUENTIAL_SEARCH 3
double times[4] = {0,0,0,0};
int binarySearch(vector<int> * v, int x);
int seqSearch(vector<int> * v, int x);
int sentinelSeqSearch(vector<int> * v, int x);
int indexSeqSearch(vector<int> * v, int x);
void searchs(vector<int> * v, int x);
void fullRandom();
void middleConcentraded();
void printResults();
void beginningConcentraded();
void endingConcentraded();
int main(){
srand(time(NULL)); // Intialize srand seed
string aux;
// Full random
system("clear");
cout << "Searching for a random number in a random generated vector.\n\n";
fullRandom();
cout << "\nPress [ENTER] to continue.\n";
getline(cin, aux);
// Concentrated in middle
system("clear");
cout << "Searching for a random number in a random generated vector, with 80% concentraded in the middle.\n\n";
middleConcentraded();
cout << "\nPress [ENTER] to continue.\n";
getline(cin, aux);
// Concentraded in beginning
system("clear");
cout << "Searching for a random number in a random generated vector, with 80% concentraded in the beginning.\n\n";
beginningConcentraded();
cout << "\nPress [ENTER] to continue.\n";
getline(cin, aux);
// Concentraded in the end
system("clear");
cout << "Searching for a random number in a random generated vector, with 80% concentraded in the ending.\n\n";
endingConcentraded();
cout << "\nPress [ENTER] to continue.\n";
getline(cin, aux);
return 0;
}
void beginningConcentraded(){
vector<int> v;
for(int i=0; i<8*1e4; i++){
v.push_back(rand()%(10000));
}
for(int i=0; i<2*1e4; i++){
v.push_back((rand() % (100000 - 10000)) + 10000);
}
sort(v.begin(), v.end());
for(int i = 0; i<1000; i++){
int findIt = rand() % 100000;
searchs(&v, findIt);
}
printResults();
}
void endingConcentraded(){
vector<int> v;
for(int i=0; i<2*1e4; i++){
v.push_back(rand() % (100000 - 10000));
}
for(int i=0; i<8*1e4; i++){
v.push_back(rand()%(100000 - 90000) + 90000);
}
sort(v.begin(), v.end());
for(int i = 0; i<1000; i++){
int findIt = rand() % 100000;
searchs(&v, findIt);
}
printResults();
}
void middleConcentraded(){
vector<int> v;
for(int i=0; i<1e4; i++){
v.push_back(rand() % 45000);
}
for(int i=0; i<8*1e4; i++){
v.push_back((rand()%(55000-45000)) + 45000 );
}
for(int i=0; i<1e4; i++){
v.push_back((rand() % (100000 - 55000)) + 55000);
}
sort(v.begin(), v.end());
for(int i = 0; i<1000; i++){
int findIt = rand() % 100000;
searchs(&v, findIt);
}
printResults();
}
void fullRandom(){
vector<int> v;
for(int i=0; i<1e5; i++){
v.push_back(rand() % 100000);
}
sort(v.begin(), v.end());
for(int i = 0; i<1000; i++){
int findIt = rand() % 100000;
searchs(&v, findIt);
}
printResults();
}
int binarySearch(vector<int> * v, int x){
int left = 0;
int right = v->size()-1;
while(left <= right){
int mid = (right+left)/2;
if(v->at(mid) == x) return mid;
if(v->at(mid) > x) right = mid - 1;
else left = mid + 1;
}
return NOT_FOUND;
}
int seqSearch(vector<int> * v, int x){
for (size_t i = 0; i < v->size(); i++) {
if (v->at(i) == x){
return i;
}
}
return NOT_FOUND;
}
int sentinelSeqSearch(vector<int> * v, int x){
v->push_back(x);
int i;
for (i=0; x!=v->at(i); i++);
v->pop_back();
if ((size_t)i < v->size()){
return i;
}
else{
return NOT_FOUND;
}
}
int indexSeqSearch(vector<int> * v, int x){
int block_size = v->size()/5;
int i,j;
for (i = block_size; i < (int) v->size(); i += block_size){
if (v->at(i) >= x) break;
}
for (j = i - block_size; j < i; j++){
if( v->at(j) == x) return j;
}
return NOT_FOUND;
}
void printResults(){
printf("Binary search average time: %lf seconds;\n", times[BINARY_SEARCH]/1000);
printf("Sequential search average time: %lf seconds;\n", times[SEQUENTIAL_SEARCH]/1000);
printf("Sequential search with sentinel average time: %lf seconds;\n", times[SENTINEL_SEQUENTIAL_SEARCH]/1000);
printf("Indexed sequential search average time: %lf seconds;\n", times[INDEXED_SEQUENTIAL_SEARCH]/1000);
times[BINARY_SEARCH] = 0;
times[SEQUENTIAL_SEARCH] = 0;
times[INDEXED_SEQUENTIAL_SEARCH] = 0;
times[SENTINEL_SEQUENTIAL_SEARCH] = 0;
}
void searchs(vector<int> * v, int findIt){
clock_t t = clock();
int x = binarySearch(v, findIt);
t = clock() - t;
times[BINARY_SEARCH]+=(double)t/CLOCKS_PER_SEC;
t = clock();
x = seqSearch(v, findIt);
t = clock() - t;
times[SEQUENTIAL_SEARCH]+=(double)t/CLOCKS_PER_SEC;
t = clock();
x = sentinelSeqSearch(v, findIt);
t = clock() - t;
times[SENTINEL_SEQUENTIAL_SEARCH]+=(double)t/CLOCKS_PER_SEC;
t = clock();
x = indexSeqSearch(v, findIt);
t = clock() - t;
times[INDEXED_SEQUENTIAL_SEARCH]+=(double)t/CLOCKS_PER_SEC;
}