-
Notifications
You must be signed in to change notification settings - Fork 0
/
external_merge_sort.cpp
226 lines (215 loc) · 5.33 KB
/
external_merge_sort.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
/*
External Sorting
Problem Statement: External Sorting is a class of algorithms used to deal with massive
amounts of data that do not fit in memory. The questions aim at implementing one such type:
K-Way merge sort algorithm to sort a very large array. This algorithm is a perfect example of
the use of divide and conquer where with limited resources large problems are tackled by
breaking the problem space into small computable subspaces and then operations are done
on them.
Input Constraints:1. A file containing a large unsorted list of integers (Will not fit in your usual
Laptop RAM).
2.Do not use any in-built data structures.
Output : A file containing non-Descending sorted list of the given integers
Languages allowed : C, C++, Python Evaluation parameters :
1. Time and Space Complexity of the algorithm
2. Efficient use of Data-Structures
Example Format:
If your input file is at ./data/input.txt And if you need your output file at ./data/ named
output.txt
*/
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int rem=0;
class Node {
public:
int data;
int fileno;
};
class Minheap {
public:
Node *arr;
int size;
public:
Minheap(Node n[],int);
int parent(int);
int leftchild(int);
int rightchild(int);
Node extractmin();
void getnewmin(Node);
void heapify(int);
};
Minheap::Minheap(Node n[],int k) {
int i;
arr=n;
size=k;
i=(k-1)/2;
for(int x=i;x>=0;x--)
heapify(x);
}
int Minheap::leftchild(int v) {
return (2*v)+1;
}
int Minheap::rightchild(int v) {
return (2*v)+2;
}
int Minheap::parent(int v) {
if(v%2 == 0)
return v/2;
else
return (v/2)-1;
}
Node Minheap::extractmin() {
return arr[0];
}
void Minheap::getnewmin(Node n) {
arr[0]=n;
heapify(0);
}
void Minheap::heapify(int x) {
int l=leftchild(x);
int r=rightchild(x);
int ldata=arr[l].data;
int rdata=arr[r].data;
int data1=arr[x].data;
int min=x;
Node temp;
if(l<size && ldata<data1)
min=l;
if(r<size && rdata<arr[min].data)
min=r;
if(min!=x) {
temp=arr[min];
arr[min]=arr[x];
arr[x]=temp;
heapify(min);
}
}
void merge(int a[],int l,int mid,int mid1,int r) {
int i=l,j=mid1,m=mid,n=r;
int b[r-l+1],k=0;
while(i<=m && j<=n) {
if(a[i]<=a[j]) {
b[k]=a[i];
k++;
i++;
}
else {
b[k]=a[j];
j++;
k++;
}
}
while(i<=m) {
b[k]=a[i];
k++;
i++;
}
while(j<=n) {
b[k]=a[j];
k++;
j++;
}
for(int x=l;x<=r;x++) {
a[x]=b[x-l];
}
}
void mergesort(int arr[],int l,int r) {
if(l<r) {
int mid=l+(r-l)/2;
mergesort(arr,l,mid);
mergesort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
}
int divideinchunks(char *filename,int recordsize) {
cout<<filename<<endl;
int i,nooffiles=0,j=0,records=0,flag=0;
string line;
ifstream file(filename);
while (getline(file, line))
records++;
file.close();
nooffiles=records/recordsize;
if(records%recordsize!=0) {
rem=records%recordsize;
flag=1;
}
FILE *fp=fopen(filename,"r");
FILE *fp1[nooffiles+1];
for(j=0;j<nooffiles;j++) {
int arr[recordsize];
for(i=0;i<recordsize;i++) {
fscanf(fp,"%d",&arr[i]);
}
mergesort(arr,0,recordsize-1);
string fname="file"+to_string(j);
char ch[1000];
strcpy(ch,fname.c_str());
fp1[j]=fopen(ch,"w");
for(i=0;i<recordsize;i++) {
fprintf(fp1[j],"%d\n",arr[i]);
}
}
if(flag==1) {
int arr[rem];
for(i=0;i<rem;i++) {
fscanf(fp,"%d",&arr[i]);
}
mergesort(arr,0,rem-1);
string fname="file"+to_string(j);
char ch[1000];
strcpy(ch,fname.c_str());
fp1[j]=fopen(ch,"w");
for(i=0;i<rem;i++) {
fprintf(fp1[j],"%d\n",arr[i]);
}
nooffiles+=1;
}
for(i=0;i<nooffiles;i++)
fclose(fp1[i]);
fclose(fp);
return nooffiles;
}
void mergefiles(char *filename,int nooffiles,int recordsize) {
FILE *fp1[nooffiles];
FILE *fp=fopen(filename,"w");
int i=0,j,totalrecords=0,x,f,c=0;
Node* n=new Node[nooffiles];
for(j=0;j<nooffiles;j++) {
string fname="file"+to_string(j);
char ch[1000];
strcpy(ch,fname.c_str());
fp1[j]=fopen(ch,"r");
fscanf(fp1[j],"%d",&n[i].data);
n[i].fileno=i;
i++;
}
Minheap heap(n,nooffiles);
Node top;
if(rem==0)
totalrecords=recordsize*nooffiles;
else
totalrecords=(recordsize*(nooffiles-1))+rem;
cout<<"totalrecords: "<<totalrecords<<endl;
for(i=0;i<totalrecords;i++) {
top=heap.extractmin();
x=top.data;
f=top.fileno;
cout<<"x: "<<x<<" f: "<<f<<endl;
fprintf(fp,"%d\n",x);
if(fscanf(fp1[f],"%d",&top.data) == EOF)
top.data=INT_MAX;
heap.getnewmin(top);
}
for(i=0;i<nooffiles;i++)
fclose(fp1[i]);
fclose(fp);
}
int main(int argc,char *argv[]) {
char *f1,*f2;
f1=argv[1];
f2=argv[2];
int nooffiles=divideinchunks(f1,10000);
mergefiles(f2,nooffiles,10000);
}