-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovidedCode.cpp
More file actions
209 lines (176 loc) · 4.68 KB
/
Copy pathprovidedCode.cpp
File metadata and controls
209 lines (176 loc) · 4.68 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
// THIS IS THE PROVIDED CODE FOR PROGRAM #2, DSA 1, SPRING 2017
#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 one string
class Data {
public:
string data;
Data(const string &s) { data = s; }
};
// Load the data from a specified input file
void loadDataList(list<Data *> &l) {
string filename;
cout << "Enter name of input file: ";
cin >> filename;
ifstream input(filename.c_str());
if (!input) {
cerr << "Error: could not open " << filename << endl;
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++) {
string line;
getline(input, line);
l.push_back(new Data(line));
}
input.close();
}
// Output the data to a specified input file
void writeDataList(const list<Data *> &l) {
string filename;
cout << "Enter name of output file: ";
cin >> filename;
ofstream output(filename.c_str());
if (!output) {
cerr << "Error: could not open " << filename << endl;
exit(1);
}
// Write the size first
int size = l.size();
output << size << endl;
// Write the data
for (list<Data *>::const_iterator ipD = l.begin(); ipD != l.end(); ipD++) {
output << (*ipD)->data << endl;
}
output.close();
}
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() {
list<Data *> theList;
loadDataList(theList);
cout << "Data loaded. Executing sort..." << endl;
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." << endl;
writeDataList(theList);
}
// -------------------------------------------------
// YOU MAY NOT CHANGE OR ADD ANY CODE ABOVE HERE !!!
// -------------------------------------------------
// You may add global variables, functions, and/or
// class defintions here if you wish.
void t4_sort(list<Data *> &l);
void t3_sort(list<Data *> &l);
void t1_t2_sort(list<Data *> &l, int n);
list<Data *> count_a[10000000];
list<Data *> sorted;
Data *p[1020000];
int bucket;
//looks at a few pieces of information based off the given list
//and chooses which sorting algorithm to use
void sortDataList(list<Data *> &l) {
int s = l.size();
if( l.size() < 102000){
t1_t2_sort(l,0);// execute version 1 of sort
}
else if ( (*l.begin())->data.length() < 8){
t3_sort(l);
}
else if((*l.begin())->data.substr(0,10) == ((*++l.begin())->data.substr(0,10)) ){
t4_sort(l);
} else {
t1_t2_sort(l,1);// execute version 2 of sort
}
}
//insertion sort for almost sorted list
void t4_sort(list<Data *> &l) {
for(list<Data *>::iterator i = l.begin(); i != l.end(); i++){
// TODO Check for super rare case where overflow changes number of digits
while(i != l.begin() and (*i)->data < (*prev(i))->data ){
iter_swap(i, prev(i));
i--;
}
}
}
//counting sort for numbers of limited range
//-1000< num < 1000
void t3_sort(list<Data *> &l){
int *c = new int[1000000];
for(list<Data *>::iterator i = l.begin(); i != l.end(); i++){
int tmp = (int) 1000*(stof((*i)->data));
p [(int) tmp ]= *i;
c[tmp]++;
}
for (int j = 0;j <999999; j++){
while (c[j]--){
sorted.push_back( p[j] );
}
}
l = sorted;
}
//comparison function
bool compare( Data *d1, Data *d2){
int i1 = 20;
int i2 = 20;
while ((d1->data)[i1] != '.'){i1--;}
while ((d2->data)[i2] != '.'){i2--;}
if(i1==i2)
return ((d1->data).compare((d2->data)) < 0);
else
return (i1 < i2);
}
//bucket sort optimized with 2 versions, versions differ by # of buckets
//sort for t2 (1,000,000 random numbers) has more buckets
//than t1 (100,000 random numbers)
void t1_t2_sort(list<Data *> &l, int n){
for (list<Data *>::iterator itr = l.begin(); itr != l.end(); itr++){
int i1= 20;
while ((*itr)-> data[i1] != '.'){ i1--;}
i1 = 20 - i1;
if (i1 < (6+n) ){
bucket = atoi( ((*itr)->data).substr(0,((6+n)-i1)).c_str());
count_a[bucket].push_back(*itr);
}
else {
count_a[0].push_back(*itr);
}
}
if (n == 0){
for (int i= 0; i < 1000000; i++){
if(count_a[i].empty())
continue;
t4_sort(count_a[i]);
sorted.splice(sorted.end(), count_a[i]);
}
}
else {
for (int i = 0; i< 10000000; i++){
if(count_a[i].empty())
continue;
t4_sort(count_a[i]);
sorted.splice(sorted.end(), count_a[i]);
}
}
l = sorted;
}