-
Notifications
You must be signed in to change notification settings - Fork 24
/
sortirovki
179 lines (160 loc) · 2.47 KB
/
sortirovki
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
#include "stdio.h"
#include "locale.h"
#include "stdlib.h"
void vvod_array(int a[], int n)
{
int i;
srand(1000);
for (i = 0; i < n; i++)
{
a[i] = rand() % 10;
}
}
void print_array(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
}
void sort1(int a[], int n)
{
int i, tmp;
int left, right;
left = 0;
right = n - 1;
while (left < right)
{
for (i = left; i < right; i++)
{
if (a[i] > a[i + 1])
{
tmp = a[i + 1];
a[i + 1] = a[i];
a[i] = tmp;
}
}
right--;
for (i = right; i > left; i--)
{
if (a[i] > a[i - 1])
{
tmp = a[i - 1];
a[i - 1] = a[i];
a[i] = tmp;
}
}
left++;
}
}
void sort2(int a[], int n)
{
int k, l;
for (int i = 1; i < n; i++)
{
k = a[i];
l = i - 1;
while (l >= 0 && a[l] > k)
{
a[l + 1] = a[l];
l = l - 1;
}
a[l + 1] = k;
}
}
int a[10];
int b[11];
void merging( int left, int mid, int right)
{
int l1, l2, i;
for (l1 = left, l2 = mid + 1, i = left; l1 <= mid && l2 <= right; i++)
{
if (a[l1] <= a[l2])
{
b[i] = a[l1++];
}
else
{
b[i] = a[l2++];
}
}
while (l1 <= mid)
{
b[i++] = a[l1++];
}
while (l2 <= right)
{
b[i++] = a[l2++];
}
for (i = left; i <= right; i++)
{
a[i] = b[i];
}
}
void sort3( int left, int right)
{
int mid;
if (left < right)
{
mid = (left + right) / 2;
sort3(left, mid);
sort3(mid + 1, right);
merging(left, mid, right);
}
}
void menu()
{
{
printf("\nMENU:\n");
printf("1. сгенерировать массив\n");
printf("2. вывести массив\n");
printf("3. отсортировать сортировкой пузырьком\n");
printf("4. отсортировать сортировкой вставками\n");
printf("5. отсортировать сортировкой слиянием\n");
printf("0. exit\n");
}
}
void main()
{
setlocale(LC_CTYPE, "Russian");
int a[10];
int n = 10;
int t = 10;
while (t != 0)
{
menu();
scanf_s("%d", &t);
switch (t)
{
case 1:
{
vvod_array(a, n);
break;
}
case 2:
{
print_array(a, n);
break;
}
case 3:
{
sort1(a, n);
print_array(a, n);
break;
}
case 4:
{
sort2(a, n);
print_array(a, n);
break;
}
case 5:
{
sort3(a, n);
print_array(a, n);
break;
}
}
}
}