-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeapsort.cpp
269 lines (228 loc) · 4.99 KB
/
Heapsort.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static int numberItems_ = -1;
struct item
{
unsigned int id;
int value;
};
void
heapifyRateMin (struct item *a, int len, int index) /// O(log n)
{
int left = 2 * index + 1;
int right = 2 * index + 2;
struct item aux;
if (left > len-1)
{
return;
}
else if (right == len)
{
if (a[index].value > a[left].value)
{ //swap left with parent
aux = a[index];
a[index] = a[left];
a[left] = aux;
}
return;
}
else if (a[index].value > a[left].value || a[index].value > a[right].value)
{
if (a[left].value > a[right].value)
{ //swap right with parent
aux = a[index];
a[index] = a[right];
a[right] = aux;
heapifyRateMin (a,len,right);
}
else
{ //swap left with parent
aux = a[index];
a[index] = a[left];
a[left] = aux;
heapifyRateMin (a,len,left);
}
}
}
void
heapifyRateMax (struct item *a, int len, int index) /// O(log n)
{
int left = 2 * index + 1;
int right = 2 * index + 2;
struct item aux;
if (left > len-1)
{
return;
}
else if (right==len)
{
if (a[index].value < a[left].value)
{ //swap left with parent
aux = a[index];
a[index] = a[left];
a[left] = aux;
}
return;
}
else if (a[index].value < a[left].value || a[index].value < a[right].value)
{
if (a[left].value < a[right].value)
{ //swap right with parent
aux = a[index];
a[index] = a[right];
a[right] = aux;
heapifyRateMax (a,len,right);
}
else
{ //swap left with parent
aux = a[index];
a[index] = a[left];
a[left] = aux;
heapifyRateMax (a,len,left);
}
}
}
void
buildHeapRate (struct item *a, int length, bool asc) /// O(n)
{
int i;
if (asc)
{
for (i = length - 1; i >= 0; i--)
{
if ( 2 * i + 1 > length - 1)
{
continue;
}
heapifyRateMax (a,length,i);
}
}
else
{
for (i = length - 1; i>= 0; i--)
{
if (2 * i + 1 > length - 1)
{
continue;
}
heapifyRateMin (a,length,i);
}
}
return;
}
void
heapsort (struct item *list, int length, bool asc) /// O(nlog n)
{
struct item aux;
int cunrrentLength = length;
buildHeapRate (list,length,asc);
if (asc)
{ //Ordem crescente.
while (cunrrentLength > 1)
{ //swap first with last
aux = list[0];
list[0] = list[cunrrentLength - 1];
list[cunrrentLength-1] = aux;
cunrrentLength--;
//heapify new heap
heapifyRateMax (list,cunrrentLength,0);
}
}
else
{ //Ordem decrescente.
while (cunrrentLength > 1)
{ //swap first with last
aux = list[0];
list[0] = list[cunrrentLength - 1];
list[cunrrentLength-1] = aux;
cunrrentLength--;
//heapify new heap
heapifyRateMin (list,cunrrentLength,0);
}
}
return;
}
void
showItens (struct item *list, FILE *file) /// O(n)
{
printf ("\nNumber of items (N): %d\n", numberItems_);
/*for (int i = 0; i < list->numberItems; i++) //Print Itens
{
printf ("Id: %d; Number: %d \n", list[i].id, list[i].value);
}*/
for (int i = 0; i < numberItems_; i++)
{
fprintf(file,"Id: %d; Value: %d \n", list[i].id, list[i].value);
}
}
struct item
*loadItems (FILE *file) ///O(n)
{
char line[1000];
char *result;
char *pch;
int i = 0;
int j = 0;
result = fgets(line, 1000, file); //Get the first line.
sscanf(result, "%d", &numberItems_);
struct item *items = (struct item *) malloc (sizeof (struct item) * numberItems_); //Instantiate itens struct.
while (!feof (file)) ///O(n)
{
result = fgets(line, 1000, file); //Get the next line.
pch = strtok(result," ");
j = 0;
while (pch != NULL) //Get the next string.
{
if (strpbrk(pch, "0123456789") == NULL) //Checking whether the string contains some number.
{
//printf("String: %s\n",pch);
}
else
{
switch (j)
{
case 0: //First string in the line.
items[i].id = atoi(pch); //Assign id to item.
break;
case 1: //Second string in the line.
items[i].value = atoi(pch); //Assign value to item.
break;
default:
break;
}
j++;
}
pch = strtok (NULL," ");
}
i++;
}
return items;
}
int
main (int argc, char **argv)
{
if (argc == 1)
{
printf ("No input\n");
return 0;
}
FILE *fileIn = fopen (argv[1], "r"); // Input file.
if (fileIn == NULL)
{
printf ("Error to open the file input\n");
return 0;
}
struct item *items = loadItems (fileIn); // Load instances. // O(n)
fclose (fileIn);
FILE *fileOut = fopen ("Output.txt", "w"); // Output file.
if (fileOut == NULL)
{
printf ("Error to create output file\n");
abort ();
}
heapsort (items,numberItems_,true);
showItens (items,fileOut);
fclose (fileOut);
return 1;
}