-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgoritmi.cpp
More file actions
94 lines (81 loc) · 1.62 KB
/
algoritmi.cpp
File metadata and controls
94 lines (81 loc) · 1.62 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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <chrono>
#include <algorithm>
using namespace std;
const long L = 100000 ;
int arr[L];
void fill_array(){
for(long i = 0; i < L; i++){
arr[i] = rand() % 10 + 1;
}
}
void print_array(){
for(long i = 0; i < L; i++){
cout << arr[i] << endl;
}
}
int linear_search(int n){
for(long i = 0; i < L; i++){
if(arr[i] == n) return i;
//cout<<i<<endl;
}
return -1;
}
void bubble_sort(){
long last = L - 1;
bool swap;
int s;
do{
swap = false;
for(long i = 0; i < last; i++){
if(arr[i] > arr[i + 1]){
swap = true;
s = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = s;
}
}
last--;
}while(last != 0 && swap);
}
long binary_search(int n){
long low = 0;
long high = L - 1;
while(low <= high){
long mid = low + (high - low) / 2;
if(arr[mid] == n)return mid;
if(arr[mid] > n){
high = mid - 1;
}else{
low = mid + 1;
}
}
return -1;
}
long millis()
{
auto now = std::chrono::system_clock::now();
// Convert to milliseconds since epoch
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
now.time_since_epoch()
).count();
//std::cout << "Milliseconds since epoch: " << milliseconds << std::endl;
return milliseconds;
}
int main(int argc, char** argv) {
srand(time(NULL));
fill_array();
long start = millis();
//bubble_sort();
long end = millis();
cout << "millisecondi LIN = " << end-start << endl;
//fill_array();
start = millis();
sort(arr, arr+L);
end = millis();
print_array();
cout << "millisecondi BIN = " << end-start;
return 0;
}