-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathf08.cpp
More file actions
153 lines (136 loc) · 2.86 KB
/
f08.cpp
File metadata and controls
153 lines (136 loc) · 2.86 KB
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
//LAB PROGRAM 8.
// Write a C++ program to read k Lists of names and merge them using k-way merge algorithm with k = 8.
//Before executing the program we need to have 8 files n1.txt, n2,txt ..... n8.txt (for k=8)
//Since this is tedious. The below program uses k = 4. Can be extened to k = 8
//As with the previous this program is performed in 3 steps.
//STEP 1. Reading k lists from k files and storing these lists in a 2Darray (list[][]) --> read_file(i);
//STEP 2. Sorting the k lists in the 2Darray --> sort_list(i);
//STEP 3. Merging the k lists into a single output list (outlist[]) --> kwaymerge() (LOGIC is extended version of Merge Sort logic)
#include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;
class coseq
{
public:
string list[8][50];
string outlist[200];
int count1[8],count2[8];
void read_file(int i);
void sort_list(int i);
void kwaymerge();
};
void error(int);
int main()
{
//system("clear");
coseq c;
for(int i=0; i<4; i++) //For each of the k lists read_file and sort_list.
{ //for k=8 use (i=0;i<8;i++)
c.count1[i] = 0;
c.read_file(i);
c.sort_list(i);
}
c.kwaymerge();
return 0;
}
void coseq::read_file(int i)
{
fstream fp;
string name;
switch(i)
{
case 0:fp.open("n1.txt",ios::in);break;
case 1:fp.open("n2.txt",ios::in);break;
case 2:fp.open("n3.txt",ios::in);break;
case 3:fp.open("n4.txt",ios::in);break;
/*case 4:fp.open("n5.txt",ios::in);break; //These 4 lines are included for k=8.
case 5:fp.open("n6.txt",ios::in);break;
case 6:fp.open("n7.txt",ios::in);break;
case 7:fp.open("n8.txt",ios::in);break;*/
}
if(!fp)
error(1);
while(fp)
{
getline(fp,name);
if(name.length()>0)
list[i][count1[i]++]=name;
}
fp.close();
}
void coseq::sort_list(int k)
{
int i,j;
string temp;
for(i=0;i<count1[k];i++)
{
for(j=i+1;j<count1[k];j++)
{
if(list[k][i]>list[k][j])
{
temp=list[k][i];
list[k][i]=list[k][j];
list[k][j]=temp;
}
}
}
}
void coseq::kwaymerge()
{
string sml;
int s_list,count3=0,strt=0,avail_list=4,avail[4],i;
for(i=0;i<4;i++)
{
avail[i]=1;
count2[i]=0;
}
while(avail_list>1)
{
if(!avail[strt])
{
strt++;
continue;
}
s_list=strt;
sml=list[strt][count2[strt]];
for(i=strt+1;i<4;i++)
{
if(!avail[i])
continue;
if(list[i][count2[i]]<sml)
{
sml=list[i][count2[i]];
s_list=i;
}
}
count2[s_list]++;
if(count2[s_list]==count1[s_list])
{
avail[s_list]=0;
avail_list--;
}
outlist[count3++]=sml;
}
for(i=0;i<4;i++)
if(avail[i])
{
for(int j=count2[i];j<count1[i];j++)
outlist[count3++]=list[i][j];
}
cout<<"\nMerged list:\n";
for(i=0;i<count3;i++)
{
if(outlist[i]==outlist[i+1])continue;
cout<<outlist[i]<<endl;
}
}
void error(int error_type)
{
switch(error_type)
{
case 1: cout<<"\nFATAL ERROR!: Unable to open the File(s)\n";
exit(0);
}
}