-
Notifications
You must be signed in to change notification settings - Fork 0
/
heapSort.c
94 lines (73 loc) · 1.36 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
#include<stdio.h>
#include<stdlib.h>
void heapsort(int[], int);
void heapify(int[], int);
void adjust(int[], int);
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("[*]Usage: heap_sort <Input File Name> <Number of elements>\n");
return 0;
}
int len = atoi(argv[2]), *arr, i;
FILE *fp, *fw;
fp = fopen(argv[1], "r");
fw = fopen("sorted_file_heap.txt", "w");
arr = (int *)malloc(len * sizeof(int));
for (i = 0 ; i < len ; i++) {
fscanf(fp, "%d", &arr[i]);
}
heapsort(arr, len);
for (i = 0 ; i < len ; i++) {
fprintf(fw, "%d\n", arr[i]);
}
free(arr);
fclose(fp);
fclose(fw);
return 0;
}
void heapsort(int *array, int n) {
int i, t;
heapify(array, n);
for (i = n - 1 ; i > 0 ; i--) {
t = array[0];
array[0] = array[i];
array[i] = t;
adjust(array, i);
}
}
void heapify(int *array, int n) {
int item, i, j, k;
for (k = 1 ; k < n ; k++) {
item = array[k];
i = k;
j = (i - 1) / 2;
while ( (i > 0) && (item > array[j]) ) {
array[i] = array[j];
i = j;
j = (i - 1) / 2;
}
array[i] = item;
}
}
void adjust(int *array, int n) {
int item, i, j;
j = 0;
item = array[j];
i = 2 * j + 1;
while (i <= n - 1) {
if (i + 1 <= n - 1) {
if (array[i] < array[i + 1]) {
i++;
}
}
if (item < array[i]) {
array[j] = array[i];
j = i;
i = 2 * j + 1;
}
else {
break;
}
}
array[j] = item;
}