This repository has been archived by the owner on Dec 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·248 lines (211 loc) · 5.52 KB
/
main.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <time.h>
#include <ctype.h>
#include "heap.h"
#include "heap_max.h"
#include "heap_min.h"
#include "heapsort.h"
#include "benchmark.h"
bool check_heap_max(int *array, int size) {
for (int i = 1; i < size; ++i) {
if (array[i] > array[(i - 1) / 2])
return false;
}
return true;
}
bool check_heap_min(int *array, int size) {
for (int i = 1; i < size; ++i) {
if (array[i] < array[(i - 1) / 2])
return false;
}
return true;
}
int input_int(char *text) {
int result, n;
do {
printf("%s", text);
n = scanf("%d", &result);
if (!n) scanf("%*[^\n]");
getchar();
} while (!n);
return result;
}
int main() {
srand(time(NULL));
char choice;
int *array = NULL, size = 0;
do {
if (size > 0) {
printf("The current array (size %d) is:\n", size);
for (int i = 0; i < size; ++i)
printf("%d ", array[i]);
printf("\n\n");
} else {
printf("The current array is empty.\n\n");
}
int n;
do {
printf("Choose an option amongst:\n");
printf("0. Initialize with a random array\n");
printf("1. Enter an array of numbers\n");
printf("2. Check if the array is a heap\n");
printf("3. Convert the array into a heap-max\n");
printf("4. Convert the array into a heap-min\n");
printf("5. Shuffle the array randomly\n");
printf("6. Pop the root of the heap\n");
printf("7. Sort the array in ascending order\n");
printf("8. Sort the array in descending order\n");
printf("9. Compare sorting algorithms' efficiency\n");
printf("Q. Quit\n");
n = scanf("%c", &choice);
if (!n) scanf("%*[^\n]");
getchar();
} while (!n);
choice = toupper(choice);
switch (choice) {
case '0':
{
do {
size = input_int("Please enter a number of integers for the array: ");
} while (size < 0);
if (array != NULL)
free(array);
if (size > 0) {
array = malloc(sizeof(int) * size);
int min = input_int("Please enter the minimum bound for the integers in the array: ");
int max;
do {
max = input_int("Please enter the maximum bound for the integers in the array: ");
} while (max <= min);
for (int i = 0; i < size; ++i)
array[i] = random() % (max - min + 1) + min;
printf("Successfully filled the array with %d random values between %d and %d.\n", size, min, max);
} else {
array = NULL;
printf("Successfully cleared the array.\n");
}
}
break;
case '1':
{
int capacity;
do {
capacity = input_int("Please enter a number of integers (max) for the array: ");
} while (capacity <= 0);
if (array != NULL)
free(array);
if (capacity > 0) {
array = malloc(sizeof(int) * capacity);
printf("Please enter %d integers at most (type a non-number to end prematurely):\n", capacity);
size = 0;
do {
n = scanf("%d", &array[size++]);
if (!n) scanf("%*[^\n]");
getchar();
} while (n && size < capacity);
printf("Successfully stored the provided array.\n");
} else {
size = 0;
array = NULL;
printf("Successfully cleared the array.\n");
}
}
break;
case '2':
if (check_heap_max(array, size)) {
if (check_heap_min(array, size)) {
printf("This array is both a heap max and a heap min (constant).\n");
} else {
printf("This array is a heap max.\n");
}
} else if (check_heap_min(array, size)) {
printf("This array is a heap min.\n");
} else {
printf("This array is neither a heap max nor a heap min.\n");
}
break;
case '3':
if (!check_heap_max(array, size)) {
Heap heap;
heap.capacity = size;
heap.size = 1;
heap.data = array;
while (heap.size < size)
heap_max_propagate_top(&heap, heap.size++);
printf("Successfully converted this array into a heap max.\n");
} else {
printf("This array is already a heap max.\n");
}
break;
case '4':
if (!check_heap_min(array, size)) {
Heap heap;
heap.capacity = size;
heap.size = 1;
heap.data = array;
while (heap.size < size)
heap_min_propagate_top(&heap, heap.size++);
printf("Successfully converted this array into a heap min.\n");
} else {
printf("This array is already a heap min.\n");
}
break;
case '5':
for (int i = size - 1; i > 0; --i) {
int j = random() % (i + 1);
int value = array[i];
array[i] = array[j];
array[j] = value;
}
printf("Successfully shuffled the array randomly.\n");
break;
case '6':
if (check_heap_max(array, size)) {
Heap heap;
heap.capacity = size;
heap.size = size;
heap.data = array;
if (heap_max_pop(&heap)) {
printf("Successfully popped the root.\n");
} else {
printf("No value to be popped.\n");
}
size = heap.size;
} else if (check_heap_min(array, size)) {
Heap heap;
heap.capacity = size;
heap.size = size;
heap.data = array;
if (heap_min_pop(&heap)) {
printf("Successfully popped the root.\n");
} else {
printf("No value to be popped.\n");
}
size = heap.size;
} else {
printf("This array is neither a heap max nor a heap min.\n");
}
break;
case '7':
inplace_heapsort_ascending(array, size);
printf("Successfully sorted the array in ascending order.\n");
break;
case '8':
inplace_heapsort_descending(array, size);
printf("Successfully sorted the array in descending order.\n");
break;
case '9':
benchmark();
break;
case 'Q':
default:
continue;
}
sleep(2);
printf("\n");
} while (choice != 'Q');
return 0;
}