-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcs235_assign18.cpp
194 lines (175 loc) · 4.82 KB
/
cs235_assign18.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
/******************************************************************************
* Program:
* Assignment 18, Merge Sort
* Brother Ercanbrack, CS 235
* Author:
* Tyler Scott
* Summary:
* This program implements a merge sort
*
* Estimated time: 8.0hrs
* Actual time: 4.0hrs
******************************************************************************/
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
/******************************************************************************
*
*****************************************************************************/
void split(list <int> &listIn, list <int> &listA, list <int> &listB)
{
for (list <int> :: iterator it = listIn.begin(); it != listIn.end();)
{
do
{
listA.push_back(*it);
it++;
}while(*it >= listA.back() && it != listIn.end());
if (it == listIn.end())
break;
do
{
listB.push_back(*it);
it++;
}while(*it >= listB.back() && it != listIn.end());
}
listIn.erase(listIn.begin(), listIn.end());
return;
}
/******************************************************************************
*
*****************************************************************************/
void merge(list <int> &listIn, list <int> &listA, list <int> &listB,
int &numSub)
{
list <int> :: iterator itA = listA.begin();
list <int> :: iterator itB = listB.begin();
numSub = 0;
while(itA != listA.end() && itB != listB.end())
{
// cerr << "before do while 1\n";
// do
// {
// cerr<< "before if1\n";
if (*itA < *itB)
{
// cerr << "in if1";
listIn.push_back(*itA);
// cout << *itA << " ";
itA++;
// numSub++;
}
else// if (*itA > *itB)
{
// cerr << "in else1";
listIn.push_back(*itB);
// cout << *itB << " ";
itB++;
// numSub++;
}
// numSub++;
//
// }while ((/**itA < listIn.back() ||*/itA != listA.end())
// (/**itB < listIn.back() || */itB != listB.end()));
if (*itA < listIn.back())// && itA != listA.end())
{
do
{
listIn.push_back(*itB);
itB++;
} while(*itB >= listIn.back() && itB != listB.end());
numSub++;
}
else if (*itB < listIn.back())// && itB != listB.end())
{
//if (*itB < listIn.back() && itA != listA.end())
do
{
listIn.push_back(*itA);
itA++;
} while(*itA >= listIn.back() && itA != listA.end());
numSub++;
}
numSub++;
}
//these are good
if (itA == listA.end() && itB != listB.end())
{
// do
// {
listIn.push_back(*itB);
itB++;
// }while (*itB >= listIn.back() && itB != listB.end());
numSub++;
}
if (itB == listB.end() && itA != listA.end())
{
do
{
listIn.push_back(*itA);
itA++;
}while (*itA >= listIn.back() && itA != listA.end());
numSub++;
}
listA.erase(listA.begin(), listA.end());
listB.erase(listB.begin(), listB.end());
return;
}
/******************************************************************************
*
*****************************************************************************/
void mergeSort(list <int> &listIn)
{
list <int> listA;
list <int> listB;
int count = 0;
do
// for (int i = 0; count != 1/*i <100*/; i++)
{
//temp = listIn;
split(listIn, listA, listB);
// for (list <int> :: iterator it1 = listA.begin(); it1 != listA.end(); it1++)
// {
// cout << *it1 << " ";
// }
// cout << endl;
// for (list <int> :: iterator it2 = listB.begin(); it2 != listB.end(); it2++)
// {
// cout << *it2 << " ";
// }
// cout << endl;
merge(listIn, listA, listB, count);
// listA.erase(listA.begin(), listA.end());
// listB.erase(listB.begin(), listB.end());
}while(count > 1);
return;
}
/******************************************************************************
* Main handles everything in this program
*****************************************************************************/
int main(int argc, char* argv[])
{
// open the file from command line
ifstream fin;
fin.open(argv[argc - 1]);
if (fin.fail())
{
cout << "open file error " << argv[argc - 1] << endl;
return 0;
}
list<int> listIn;
int dataIn;
while(fin >> dataIn)
{
listIn.push_back(dataIn);
}
fin.close();
mergeSort(listIn);
for (list <int> :: iterator it = listIn.begin(); it != listIn.end(); it++)
{
cout << *it << " ";
}
cout << endl;
return 0;
}