-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgosi_LV1
131 lines (98 loc) · 2.43 KB
/
Algosi_LV1
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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void generiranjeArray(float V[], int n, float dg, float gg );
int sekvencijalnoPretrazivanje( float V[], int n, float x );
void sortiranje( float V[], int n );
void swap(float *x, float *y);
int partition(float V[], int low, int high);
void quickSort(float V[], int low, int high);
int binarnoPretrazivanje(float V[], int n, float x);
int main()
{
time_t t1, t2, t3, t4, t5, t6;
int n;
float *A;
printf("Unesite n velicinu polja:\n");
scanf("%d", &n);
A = (float * ) malloc(n * sizeof(float));
generiranjeArray(A, n, 5, 300);
float trazeni_br = 7;
t1 = clock();
sekvencijalnoPretrazivanje(A, n, trazeni_br);
t2 = clock();
printf("\nVrijeme izmedu sekvecijalnog pr je: %ld ms", (t2 - t1));
t3 = clock();
sortiranje(A, n);
t4 = clock();
printf("\nVrijeme izmedu sortiranja je: %ld ms", (t4 - t3));
t5 = clock();
binarnoPretrazivanje(A, n, trazeni_br);
t6 = clock();
printf("\nVrijeme izmedu binarnog pr je: %ld ms", (t6 - t5));
return 0;
free(A);
}
void generiranjeArray(float V[], int n, float dg, float gg ) {
srand(time(NULL));
for(int i = 0; i < n; i ++) {
V[i] = dg + ((float) rand() / RAND_MAX) * (gg - dg);
}
}
int sekvencijalnoPretrazivanje( float V[], int n, float x ){
for(int i = 0; i < n; i++){
if(V[i] == x){
return i;
}
}
return -1;
}
void swap(float *a, float *b)
{
float temp = *a;
*a = *b;
*b = temp;
}
int partition(float V[], int low, int high)
{
float pivot = V[high];
int i = low - 1;
for (int j = low; j <= high - 1; j++) {
if (V[j] < pivot) {
i++;
swap(&V[i], &V[j]);
}
}
swap(&V[i+1], &V[high]);
return i+1;
}
void quickSort(float V[], int low, int high)
{
if (low < high) {
int pi = partition(V, low, high);
quickSort(V, low, pi - 1);
quickSort(V, pi + 1, high);
}
}
void sortiranje(float V[], int n)
{
quickSort(V, 0, n-1);
}
int binarnoPretrazivanje(float V[], int n, float x)
{
int left = 0;
int right = n - 1;
while (left <= right) {
int middle = (left + right) / 2;
if (V[middle] == x) {
return middle;
}
else if (V[middle] < x) {
left = middle + 1;
}
else {
right = middle - 1;
}
}
return -1;
}