-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheapsort.c
148 lines (96 loc) · 3.42 KB
/
heapsort.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
#include <sys/times.h>
#include <signal.h>
#include "record.h"
/* Heapify function used to "transform" the records array into a heap array. */
void heapify(Record* records, long size, long index, int attribute, int (*compare)(const Record *, const Record *, int))
{
long largest = index;
long left_child = 2 * index + 1;
long right_child = 2 * index + 2;
if (left_child < size && compare(&records[left_child], &records[largest], attribute) > 0) {
largest = left_child;
}
if (right_child < size && compare(&records[right_child], &records[largest], attribute) > 0) {
largest = right_child;
}
if (largest != index) {
swap(&records[index], &records[largest]);
heapify(records, size, largest, attribute, compare);
}
}
/* Heap Sort function. Big thanks to Introduction to Algorithms by Cormen. */
void heap_sort(Record* records, long size, int attribute, int (*compare)(const Record *, const Record *, int))
{
long i = 0;
for (i = size / 2 - 1; i >= 0; i--) {
heapify(records, size, i, attribute, compare);
}
for (i = size - 1; i >= 0; i--) {
swap(&records[0], &records[i]);
heapify(records, i, 0, attribute, compare);
}
}
int main(int argc, char* argv[])
{
/* get the name of the file with the records, the attribute in which we want
the records to be sorted, and the indices of the records that shall be
sorted */
char* inputfile = argv[1];
int attribute = atoi(argv[2]);
long left = atoi(argv[3]);
long right = atoi(argv[4]);
int pipe_fd = atoi(argv[5]);
int pipe_for_time = atoi(argv[6]);
pid_t coach_pid = atoi(argv[7]);
struct tms tb1, tb2;
double t1, t2, ticspersec;
ticspersec = (double) sysconf(_SC_CLK_TCK);
t1 = (double) times(&tb1);
/* calculate how many records the sorting routine will sort */
long records_to_sort = right - left + 1;
/* open the records file */
FILE* records_file = fopen (inputfile, "rb");
if (records_file == NULL) {
printf("\n\nCannot open binary file. Error in heapsort.c\n\n");
exit(-2);
}
/* go to the start of the records we want to sort */
fseek(records_file, left * sizeof(Record), 0);
/* get the records we want to sort inside an array */
long i = 0;
Record* records = malloc(sizeof(Record) * records_to_sort);
for (i = 0; i < records_to_sort; i++) {
fread(&(records[i]), sizeof(Record), 1, records_file);
}
/* close the records file because we do not need it anymore */
fclose(records_file);
/* heap sort */
heap_sort(records, records_to_sort, attribute, comparator);
// for (i = 0; i < records_to_sort; i++) {
//
// print_record(records[i]);
//
// }
long records_sent = 0;
/* write records to pipes */
for (i = 0; i < records_to_sort; i += BATCH) {
// printf("heapsort: write(): i = %ld\n", i);
if (records_sent + BATCH < records_to_sort) {
write(pipe_fd, &records[i], BATCH * sizeof(Record));
} else {
write(pipe_fd, &records[i], (records_to_sort - records_sent) * sizeof(Record));
}
}
free(records);
/* calculate real time for sorter */
t2 = (double) times(&tb2);
double time = 0.0;
/* measure real time */
time = ((double) t2 - t1) / ticspersec;
// /* measure cpu_time */
// time = ((double) ((tb2.tms_utime + tb2.tms_stime) - (tb1.tms_utime + tb1.tms_stime))) / ticspersec;
write(pipe_for_time, &time, sizeof(double));
/* send signal SIGUSR2 to the coach */
kill(coach_pid, SIGUSR2);
return 0;
}