This repository was archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSorting.cpp
More file actions
287 lines (211 loc) · 7.88 KB
/
Copy pathSorting.cpp
File metadata and controls
287 lines (211 loc) · 7.88 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*-------------------------------------------------------------------------+
| Name: Jerome Richards |
| Course: CNS-2400 F01 |
| |
| Project: Sorting - Insertion Sort, Shell Sort, and Quick Sort |
| File: Sorting.cpp |
| |
| Compare the performance of the three sorting techniques |
+-------------------------------------------------------------------------*/
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <time.h>
#include "List.h"
using namespace std;
enum sortTYPE { INSERTION, SHELL, QUICK };
struct sortINFO
{
clock_t _elapsedClock;
clock_t _elapsedTime;
string _outFile;
};
struct procINFO
{
int _fileNum;
int _numItems;
sortINFO _sorts[ 3 ];
};
void dispPrologue();
bool openFile( ifstream& inFile );
bool writeFile( string& fileName, int aData[] );
bool readData( ifstream& inFile, int& tmpData );
bool writeSorted( string fileName, SortedList<int>* tmpList );
void dispStats( procINFO tmpStats[], int max );
int main( int argc, char* argv[] )
{
ifstream inFile;
int tmpData;
List<int>* dataList;
SortedList<int>* sortedList;
const int maxFILES = 10;
int ptr = 0;
procINFO procStats[ maxFILES ];
clock_t start, end;
stringstream charNum;
dispPrologue();
while( openFile( inFile ) && ptr < maxFILES )
{
dataList = new(nothrow) List<int>();
if( ! dataList )
{
cout << "ERROR: cannot instantiate the list. Press ENTER to end program.";
cin.get();
exit( 1 );
}
cout << "Processing file. . .";
procStats[ ptr ]._fileNum = ptr+1;
procStats[ ptr ]._numItems = 0;
charNum.str( "" );
charNum << procStats[ ptr ]._fileNum;
while( ! inFile.eof() )
{
if( readData( inFile, tmpData ) )
{
procStats[ ptr ]._numItems++;
dataList->insert( tmpData );
}
}
cout << "done." << endl;
cout << "Total items read: " << procStats[ ptr ]._numItems << endl << endl;
cout << "Sorting the list with Insertion Sort. . .";
// create a SortedList object with the copy constructor to start with initial items
sortedList = new(nothrow) SortedList<int>( dataList );
// sort the list using the insertionSort method
start = clock();
sortedList->insertionSort();
end = clock();
procStats[ ptr ]._sorts[ INSERTION ]._elapsedClock = end - start;
procStats[ ptr ]._sorts[ INSERTION ]._elapsedTime = (end - start) / CLK_TCK;
procStats[ ptr ]._sorts[ INSERTION ]._outFile = "I" + charNum.str() + ".txt";
cout << "writing output file. . .";
writeSorted( procStats[ ptr ]._sorts[ INSERTION ]._outFile, sortedList );
delete sortedList;
sortedList = NULL;
cout << "done." << endl << endl;
cout << "Sorting the list with Shell Sort. . .";
// create a SortedList object with the copy constructor to start with initial items
sortedList = new(nothrow) SortedList<int>( dataList );
// sort the list using the shellSort method
start = clock();
sortedList->shellSort();
end = clock();
procStats[ ptr ]._sorts[ SHELL ]._elapsedClock = end - start;
procStats[ ptr ]._sorts[ SHELL ]._elapsedTime = (end - start) / CLK_TCK;
procStats[ ptr ]._sorts[ SHELL ]._outFile = "S" + charNum.str() + ".txt";
cout << "writing output file. . .";
writeSorted( procStats[ ptr ]._sorts[ SHELL ]._outFile, sortedList );
delete sortedList;
sortedList = NULL;
cout << "done." << endl << endl;
cout << "Sorting the list with Quick Sort. . .";
// create a SortedList object with the copy constructor to start with initial items
sortedList = new(nothrow) SortedList<int>( dataList );
// sort the list using the quickSort method
start = clock();
sortedList->quickSort();
end = clock();
procStats[ ptr ]._sorts[ QUICK ]._elapsedClock = end - start;
procStats[ ptr ]._sorts[ QUICK ]._elapsedTime = (end - start) / CLK_TCK;
procStats[ ptr ]._sorts[ QUICK ]._outFile = "Q" + charNum.str() + ".txt";
cout << "writing output file. . .";
writeSorted( procStats[ ptr ]._sorts[ QUICK ]._outFile, sortedList );
delete sortedList;
sortedList = NULL;
cout << "done." << endl << endl;
// release the source list (original unsorted items)
delete dataList;
dataList = NULL;
inFile.close();
inFile.clear();
// keep track of how many files we've processed
ptr++;
}
// display the statistics if any files were processed
if( ptr )
dispStats( procStats, ptr );
return 0;
}
void dispStats( procINFO tmpStats[], int max )
{
cout << endl << endl;
cout << "File# Items Sort Clock Time outFile" << endl;
cout << "----- -------- --------- ------- ------- -------" << endl;
// 99 99999999 Insertion 9999999 999.999 I10.txt
for( int x = 0; x < max; x++ )
{
cout << " " << setw(2) << tmpStats[x]._fileNum << " ";
cout << setw(8) << tmpStats[x]._numItems << " ";
cout << "Insertion" << " ";
cout << setw(7) << tmpStats[x]._sorts[ INSERTION ]._elapsedClock << " ";
cout << setw(7) << tmpStats[x]._sorts[ INSERTION ]._elapsedTime << " ";
cout << tmpStats[ x ]._sorts[ INSERTION ]._outFile << endl;
cout << " ";
cout << "Shell " << " ";
cout << setw(7) << tmpStats[x]._sorts[ SHELL ]._elapsedClock << " ";
cout << setw(7) << tmpStats[x]._sorts[ SHELL ]._elapsedTime << " ";
cout << tmpStats[ x ]._sorts[ SHELL ]._outFile << endl;
cout << " ";
cout << "Quick " << " ";
cout << setw(7) << tmpStats[x]._sorts[ QUICK ]._elapsedClock << " ";
cout << setw(7) << tmpStats[x]._sorts[ QUICK ]._elapsedTime << " ";
cout << tmpStats[ x ]._sorts[ QUICK ]._outFile << endl << endl;
}
cout << endl << endl << "Press ENTER to continue...";
cin.get();
}
bool writeSorted( string fileName, SortedList<int>* tmpList )
{
bool lOkay = false;
ofstream outFile( fileName.c_str(), ios_base::out );
int pos = 0;
int tmpData;
while( tmpList->retrieve( pos, tmpData ) )
{
outFile << tmpData << endl;
pos++;
}
outFile.close();
lOkay = true;
return lOkay;
}
bool readData( ifstream& inFile, int& tmpData )
{
bool lOkay = false;
inFile >> tmpData;
lOkay = ! inFile.fail();
return lOkay;
}
bool openFile( ifstream& inFile )
{
bool lOpen = false;
string fileName;
while( ! lOpen )
{
cout << "Enter file name: (blank to exit): ";
getline( cin, fileName );
if( ! fileName.length() )
{
cout << endl << endl << "Press ENTER to exit program.";
cin.get();
return false;
}
inFile.open( fileName.c_str() );
if( !( lOpen = inFile.good() ) )
{
cout << endl << "ERROR: could not open data file " << fileName << endl;
inFile.clear();
}
}
return lOpen;
}
void dispPrologue()
{
system( "cls" );
cout << " Name: Jerome Richards" << endl;
cout << " Course: CNS-2400 F01" << endl;
cout << "Project: Sorting (prog-05)" << endl;
cout << endl << endl;
}