-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab_2_2.c
More file actions
164 lines (160 loc) · 3.63 KB
/
Lab_2_2.c
File metadata and controls
164 lines (160 loc) · 3.63 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
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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
void BubbleSort(float *mas, int size)
{
long int i = 0, j = 0;
char noSwap = 1;
float temp = 0.0;
for (i = size - 1; i >= 0; i--)
{
noSwap = 1;
for (j = 0; j < i; j++)
{
if (mas[j] > mas[j + 1])
{
temp = mas[j];
mas[j] = mas[j + 1];
mas[j + 1] = temp;
noSwap = 0;
}
}
if (noSwap == 1)
break;
}
}
void InsertSort(float *mas, int size)
{
long int i = 0, loc = 0;
float temp = 0.0;
for (i = 1; i < size; i++)
{
temp = mas[i];
loc = i - 1;
while(loc >= 0 && mas[loc] > temp)
{
mas[loc + 1] = mas[loc];
loc--;
}
mas[loc + 1] = temp;
}
}
void QuickSort(float *mas, int size)
{
int i = 0, j = size - 1;
float mid = mas[(int)(size / 2)];
float temp = 0.0;
do {
while(mas[i] < mid)
{
i++;
}
while(mas[j] > mid)
{
j--;
}
if (i <= j)
{
temp = mas[i];
mas[i] = mas[j];
mas[j] = temp;
i++;
j--;
}
} while (i <= j);
if(j > 0) {
QuickSort(mas, j + 1);
}
if (i < size) {
QuickSort(&mas[i], size - i);
}
}
int main()
{
long int n = 0, i = 0, j = 0, loc = 0, comm = 0;
float mx = 0.0, mn = 0.0, temp = 0.0;
float readn = 0.0;
float* mas;
float* masC;
FILE *file = NULL;
clock_t t_start;
clock_t t_end;
file = fopen("output_1.txt", "r");
if (file == NULL)
{
printf("No file named 'output_1.txt' found. Press any key to exit.");
getch();
return 0;
}
fscanf(file, "%d", &n);
mas = (float*)malloc(n * sizeof(float));
masC = (float*)malloc(n * sizeof(float));
for (i = 0; i < n; i++)
{
fscanf(file, "%f", &readn);
mas[i] = readn;
masC[i] = readn;
}
fclose(file);
while (comm != 3)
{
printf("Select command:\n1 - Print numbers\n2 - Sort numbers\n3 - Exit\n");
scanf("%d", &comm);
switch(comm)
{
case 1:
{
for (i = 0; i < n; i++)
{
printf("%lf\n", masC[i]);
}
printf("\n");
break;
}
case 2:
{
t_start = clock();
BubbleSort(masC, n);
t_end = clock();
file = fopen("output_2_1.txt", "w");
for (i = 0; i < n; i++)
{
fprintf(file, "%lf\n", masC[i]);
masC[i] = mas[i];
}
fclose(file);
printf("Bubble sort time: %lf seconds.\n", (float)((t_end - t_start) / CLOCKS_PER_SEC));
t_start = clock();
InsertSort(masC, n);
t_end = clock();
file = fopen("output_2_2.txt", "w");
for (i = 0; i < n; i++)
{
fprintf(file, "%lf\n", masC[i]);
masC[i] = mas[i];
}
fclose(file);
printf("Insert sort time: %lf seconds.\n", (float)((t_end - t_start) / CLOCKS_PER_SEC));
t_start = clock();
QuickSort(masC, n);
t_end = clock();
file = fopen("output_2_3.txt", "w");
for (i = 0; i < n; i++)
{
fprintf(file, "%lf\n", masC[i]);
masC[i] = mas[i];
}
fclose(file);
printf("Quick sort time: %lf seconds.\n", (float)((t_end - t_start) / CLOCKS_PER_SEC));
printf("Results of sorts saved into files 'output_2_1.txt', 'output_2_2.txt', 'output_2_3.txt'.\n\n");
break;
}
default:
break;
}
}
free(mas);
free(masC);
return 0;
}