-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource(2).c
153 lines (137 loc) · 2.95 KB
/
Source(2).c
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <time.h>
void bubble(unsigned char* mas, int n)
{
int i = 0, j = 0;
unsigned char temp = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (mas[j] > mas[j + 1])
{
temp = mas[j];
mas[j] = mas[j + 1];
mas[j + 1] = temp;
}
}
}
}
void quick(unsigned char* mas, int n)
{
int i = 0;
int j = n - 1;
int mid = mas[n / 2];
do
{
while (mas[i] < mid) i++;
while (mas[j] > mid) j--;
if (i <= j)
{
int tmp = mas[i];
mas[i] = mas[j];
mas[j] = tmp;
i++;
j--;
}
}
while (i <= j);
if (j > 0) quick(mas, j + 1);
if (i < n) quick(&mas[i], n - i);
}
void insert(unsigned char* mas, int n)
{
unsigned char temp = 0;
for (int i = 0; i < n; i++)
{
temp = mas[i];
int j = i;
while ((j > 0) && (mas[j - 1] > temp))
{
mas[j] = mas[j - 1];
j--;
}
mas[j] = temp;
}
}
int main()
{
setlocale(LC_ALL, "Russian");
int n = 0;
int i = 0;
unsigned char* mas;
int choice = 0;
FILE* file;
file = fopen("C:/Users/79527/source/repos/Lab2(1)/Lab2(1)/numbers.txt", "r+");
fscanf_s(file, "%d", &n);
mas = (unsigned char*)malloc(n * sizeof(unsigned int));
for (i = 1; i < n+1; i++)
{
fscanf_s(file, "%u\n", &mas[i-1]);
}
printf("Main menu:\n");
printf("\n");
printf("Ïå÷àòü [1]\n");
printf("Ñîðòèðîâêà ïóçûð¸ê [2]\n");
printf("Áûñòðàÿ ñîðòèðîâêà [3]\n");
printf("Ñîðòèðîâêà âñòàâêîé [4]\n");
printf("Âûõîä [5]\n\n");
printf("Ââîä: ");
scanf_s("%d", &choice);
switch (choice)
{
case 1:
printf("\n");
for (i = 0; i < n; i++)
{
printf("%u\n", mas[i]);
}
break;
case 2:
printf("\n");
unsigned int start1 = clock();
bubble(mas, n);
for (i = 0; i < n; i++)
{
printf("%u\n", mas[i]);
}
unsigned int end1 = clock();
unsigned int search1 = end1 - start1;
printf("\nÂðåìÿ ðàáîòû: %u ìñ\n", search1);
break;
case 3:
printf("\n");
unsigned int start2 = clock();
quick(mas, n);
for (i = 0; i < n; i++)
{
printf("%u\n", mas[i]);
}
unsigned int end2 = clock();
unsigned int search2 = end2 - start2;
printf("\n\nÂðåìÿ ðàáîòû: %u ìñ\n", search2);
break;
case 4:
printf("\n");
unsigned int start3 = clock();
insert(mas, n);
for (i = 0; i < n; i++)
{
printf("%u\n", mas[i]);
}
unsigned int end3 = clock();
unsigned int search3 = end3 - start3;
printf("\n\nÂðåìÿ ðàáîòû: %u ìñ\n", search3);
break;
case 5:
free(mas);
fclose(file);
return 0;
}
free(mas);
fclose(file);
return 0;
}