-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_sort_files.c
157 lines (108 loc) · 2.85 KB
/
merge_sort_files.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
#include <stdio.h>
//Shorthand to read an integer
int Read(int * p1, FILE *fp)
{
return (fread(p1, sizeof(int), 1, fp));
}
//Function to merge consecutive pairs of subarrays of given length in the input file and write it into the temporary file
int Merge(FILE *f1, FILE *f2, FILE *f3, int mult)
{
int i = 0;
int j = 0;
int x1,x2;
int bool1 = Read(&x1, f1);
int bool2 = Read(&x2, f2);
//loop runs through the left and right subarrays and writes the smaller element into the temporary file
while (i<mult && j<mult)
{
if(!(bool1&&bool2))
{
break;
}
if(x1>x2)
{
fwrite(&x2, sizeof(int), 1, f3);
j++;
if(j<mult)
{
bool2 = Read(&x2, f2);
}
}
else
{
fwrite(&x1, sizeof(int), 1, f3);
i++;
if(i<mult)
{
bool1 = Read(&x1, f1);
}
}
}
//Writes either if the remaining list if one list encounters EOF or is finished
while(j<mult && bool2)
{
fwrite(&x2, sizeof(int), 1, f3);
j++;
if(j<mult)
{
bool2 = Read(&x2, f2);
}
}
while(i<mult && bool1)
{
fwrite(&x1, sizeof(int), 1, f3);
i++;
if(i<mult)
{
bool1 = Read(&x1, f1);
}
}
//Returns EOF status
return bool1&&bool2;
}
//Recursive function to sort and rewrite the input array
void MergeFile(FILE *f1, FILE *f2, FILE *f3, int mult, long len)
{
fseek(f2, (mult)*sizeof(int), SEEK_SET);
int bool = 1;
//Merge the files in batches until EOF is encountered
while(bool)
{
bool = Merge(f1, f2, f3, mult);
fseek(f1, (mult)*sizeof(int), SEEK_CUR);
fseek(f2, (mult)*sizeof(int), SEEK_CUR);
}
fclose(f2);
//Seek back at the end of one set of batches and rewrite the input array
fseek(f1, 0, SEEK_SET);
fseek(f3, 0, SEEK_SET);
int x;
while(fread(&x, sizeof(int), 1, f3))
{
fwrite(&x, sizeof(int), 1, f1);
}
fclose(f1);fclose(f3);
//End condition
if(mult > len)
{
return;
}
f1 = fopen("input1.txt", "r+");
f2 = fopen("input1.txt", "r");
f3 = fopen("temp1.txt", "w+");
MergeFile(f1, f2, f3, mult*2, len);
}
int main()
{
FILE *f1, *f2, *f3;
f1 = fopen("input1.txt", "r+");
f2 = fopen("input1.txt", "r");
f3 = fopen("temp1.txt", "r+");
//Determine the size of the input array
fseek(f1, 0, SEEK_END);
long len = ftell(f1)/sizeof(int);
fseek(f1, 0, SEEK_SET);
MergeFile(f1,f2, f3, 1, len);
printf("Done!\n");
return 0;
}