-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.cpp
executable file
·262 lines (230 loc) · 6.33 KB
/
Code.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
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
/*Hadassah Yanofsky
DSA 1 - Fall 2019
Cooper Union */
// THIS IS THE PROVIDED CODE FOR PROGRAM #2, DSA 1, FALL 2019
#include <iostream>
#include <fstream>
#include <sstream>
#include <list>
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdlib>
using namespace std;
// A simple class; each object holds four public fields
class Data {
public:
string lastName;
string firstName;
string ssn;
};
// Load the data from a specified input file
void loadDataList(list<Data*>& l, const string& filename) {
ifstream input(filename);
if (!input) {
cerr << "Error: could not open " << filename << "\n";
exit(1);
}
// The first line indicates the size
string line;
getline(input, line);
stringstream ss(line);
int size;
ss >> size;
// Load the data
for (int i = 0; i < size; i++) {
getline(input, line);
stringstream ss2(line);
Data* pData = new Data();
ss2 >> pData->lastName >> pData->firstName >> pData->ssn;
l.push_back(pData);
}
input.close();
}
// Output the data to a specified output file
void writeDataList(const list<Data*>& l, const string& filename) {
ofstream output(filename);
if (!output) {
cerr << "Error: could not open " << filename << "\n";
exit(1);
}
// Write the size first
int size = l.size();
output << size << "\n";
// Write the data
for (auto pData : l) {
output << pData->lastName << " "
<< pData->firstName << " "
<< pData->ssn << "\n";
}
output.close();
}
// Sort the data according to a specified field
// (Implementation of this function will be later in this file)
void sortDataList(list<Data*>&);
// The main function calls routines to get the data, sort the data,
// and output the data. The sort is timed according to CPU time.
int main() {
string filename;
cout << "Enter name of input file: ";
cin >> filename;
list<Data*> theList;
loadDataList(theList, filename);
cout << "Data loaded.\n";
cout << "Executing sort...\n";
clock_t t1 = clock();
sortDataList(theList);
clock_t t2 = clock();
double timeDiff = ((double)(t2 - t1)) / CLOCKS_PER_SEC;
cout << "Sort finished. CPU time was " << timeDiff << " seconds.\n";
cout << "Enter name of output file: ";
cin >> filename;
writeDataList(theList, filename);
return 0;
}
// -------------------------------------------------
// YOU MAY NOT CHANGE OR ADD ANY CODE ABOVE HERE !!!
// -------------------------------------------------
Data** all;
int size;
class Data2 {
public:
Data* ptr;
char ln1;
char fn1;
const char* lname;
const char* fname;
const char* ssn;
Data2(Data* d, const char* ln, const char* fn, const char* ss, char l1, char f1)
: ptr{ d }, lname{ ln }, fname{ fn }, ssn{ ss }, ln1{ l1 }, fn1{ f1 } {}
Data2() {};
};
bool compare5 (Data2& a, Data2& b) {
if (a.ln1 == b.ln1) {
if (strcmp(a.lname, b.lname) == 0) {
if (a.fn1 == b.fn1) {
if (strcmp(a.fname, b.fname) == 0) {
return (strcmp(a.ssn, b.ssn) < 0);
}
else {
return (strcmp(a.fname, b.fname) < 0);
}
}
else {
return (a.fn1 < b.fn1);
}
}
else {
return (strcmp(a.lname, b.lname) < 0);
}
}
else {
return (a.ln1 < b.ln1);
}
}
Data2 all2[1020000];
bool compare1(Data* & x, Data* & y) {
if (((((x)->lastName).compare(0,1,(y)->lastName,0,1))!= 0) || ((((x)->lastName).compare((y)->lastName)) != 0)) {
return ((((x)->lastName).compare((y)->lastName)) < 0);
}
else if (((((x)->firstName).compare(0, 1, (y)->firstName, 0, 1)) != 0) || ((((x)->firstName).compare((y)->firstName)) != 0)) {
return ((((x)->firstName).compare((y)->firstName)) < 0);
}
else {
return ((((x)->ssn).compare((y)->ssn)) < 0);
}
}
bool compare3(Data*& x, Data*& y) {
if ((((x)->lastName).compare((y)->lastName)) == 0 && (((x)->firstName).compare((y)->firstName)) == 0) {
return true;
}
return false;
}
bool compare4(Data*& x, Data*& y) {
return ((((x)->ssn).compare((y)->ssn)) < 0);
}
void bin() {
int first, last;
first = 0;
for (int i = 0; i < size; i++) {
if (i != (size - 1)) {
if (compare3(all[i], all[i + 1])) {
continue;
}
}
last = i+1;
sort(all + first, all + last, compare4);
first = i + 1;
}
}
void radix() {
const int BUCKETS = 100000;
const int k = 300;
int temp1;
Data** bucket = new Data * [BUCKETS * k];
int* count = new int[BUCKETS];
//count = { 0 };
for (int i = 0; i < size; i++) {
temp1 = ((int)(all[i]->ssn)[0] - '0') * 1000;
temp1 += ((int)(all[i]->ssn)[1] - '0') * 100;
temp1 += ((int)(all[i]->ssn)[2] - '0') * 10;
temp1 += ((int)(all[i]->ssn)[4] - '0') * 1;
bucket[(temp1*k) + count[temp1]] = all[i];
count[temp1]++;
}
for (int i = 0; i < BUCKETS; i++) {
sort(bucket + (i*k), bucket + ((i*k)+count[i]), compare4);
}
int index = 0;
for (int i = 0; i < BUCKETS; i++) {
for (int j = 0; j < count[i]; j++) {
all[index++] = bucket[(i*k) + j];
}
}
}
void sortDataList(list<Data*>& l) {
size = l.size();
all = new Data* [size];
int i = 0;
int j = 0;
list<Data*>::iterator iterator;
//Test 4
if (((*next(l.begin(), 5))->lastName).compare((*next(l.begin(), 350))->lastName) == 0 && ((*next(l.begin(), 10))->firstName).compare((*next(l.begin(), 320))->firstName) == 0) {
for (iterator = l.begin(); iterator != l.end(); ++iterator) {
all[i++] = *iterator;
}
radix();
i = 0;
for (iterator = l.begin(); iterator != l.end(); ++iterator) {
*iterator = all[i++];
}
}
//Test 3
else if(((*next(l.begin(), 5))->lastName).compare((*next(l.begin(), 350))->lastName)<=0 && ((*next(l.begin(), 10))->lastName).compare((*next(l.begin(), 320))->lastName)<=0){
for (iterator = l.begin(); iterator != l.end(); ++iterator) {
all[i++] = *iterator;
}
bin();
i = 0;
for (iterator = l.begin(); iterator != l.end(); ++iterator) {
*iterator = all[i++];
}
}
//Test 2 and 1
else {
//sort(all, all + size, compare1);
for (auto i = l.begin(); i != l.end(); i++, j++) {
all2[j] = Data2((*i), ((*i)->lastName).data(), ((*i)->firstName).data(),
((*i)->ssn).data(), (*i)->lastName[0], (*i)->firstName[0]);
}
sort(all2, all2+j, compare5);
j = 0;
for (auto i = l.begin(); i != l.end(); i++, j++) {
*i = all2[j].ptr;
}
}
}