-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
109 lines (105 loc) · 2.34 KB
/
main.c
File metadata and controls
109 lines (105 loc) · 2.34 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
// Ñðàâíåíèå ìåòîäîâ ñîðòèðîâêè
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
int pq = 0, ps = 0, sq = 0, ss = 0, n = 10000;
void sorts(double *a, int n)
{
if (n == 1){return;}
int i, k = 0;
double max = INT_MIN, t = 0;
for(i = 0; i < n; i++)
{
if (a[i] > max){max = a[i]; k = i;}
ss++;
}
if(k != 0){t = a[0]; a[0] = a[k]; a[k] = t; ps++;}
sorts(a + 1, n - 1);
}
void swap(double *mas, int i, int j)
{
double t;
t = mas[i];
mas[i] = mas[j];
mas[j] = t;
}
int min(double a, double b, double c)
{
if(a <= b && a <= c){return 1;}
else if(b <= a && b <= c){return 2;}
else{return 3;}
}
void sortq(double *a, int n)
{
if(n < 2){return;}
int i = n/2 - 1;
for(i = n/2 - 1; i > -1; i--)
{
sq += 2;
if(min(a[i], a[2 * i], a[2 * i + 1]) == 2){swap(a, i, 2 * i); pq++;}
else if(min(a[i], a[2 * i], a[2 * i + 1]) == 3){swap(a, i, 2 * i + 1); pq++;}
}
swap(a, 0, n - 1); pq++;
sortq(a, n - 1);
}
void rnd(double *a, double *b)
{
int i;
for(i = 0; i < n; i++)
{
a[i] = rand() * ((rand() & 1) ? -1 : 1) / 100.0;
b[i] = a[i];
//printf("%f ", a[i]);
}
}
void up(double *a, double *b)
{
int i;
for(i = 0; i < n; i++)
{
a[i] = i / 100.0;
b[i] = a[i];
}
}
void down(double *a, double *b)
{
int i;
for(i = n; i > 0; i--)
{
a[n - i] = i / 100.0;
b[n - i] = a[n - i];
}
}
int main(void)
{
srand(time(NULL));
int i;
double *masq, *mass;
masq = malloc(sizeof(double) * n);
mass = malloc(sizeof(double) * n);
down(mass, masq);
sortq(masq, n);
sorts(mass, n);
printf("\n");
printf("%d %d \n%d %d", ss, sq, ps, pq);
pq = 0; ps = 0; sq = 0; ss = 0;
up(mass, masq);
sortq(masq, n);
sorts(mass, n);
printf("\n");
printf("%d %d \n%d %d", ss, sq, ps, pq);
pq = 0; ps = 0; sq = 0; ss = 0;
rnd(mass, masq);
sortq(masq, n);
sorts(mass, n);
printf("\n");
printf("%d %d \n%d %d", ss, sq, ps, pq);
pq = 0; ps = 0; sq = 0; ss = 0;
rnd(mass, masq);
sortq(masq, n);
sorts(mass, n);
printf("\n");
printf("%d %d \n%d %d", ss, sq, ps, pq);
return 0;
}