-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix_sort.cpp
More file actions
149 lines (124 loc) · 4.06 KB
/
Copy pathradix_sort.cpp
File metadata and controls
149 lines (124 loc) · 4.06 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
// # *********************************************************
// Program: radix_sort.cpp
// Course: CCP6214 Algorithm Design and Analysis
// Lecture Class: TC2L
// Tutorial Class: TT5L
// Trimester: 2610
// Member_1: Hew Wee Bo | hewweebo@gmail.com | 0128803121
// Member_2: ID | NAME | EMAIL | PHONE
// Member_3: ID | NAME | EMAIL | PHONE
// Member_4: ID | NAME | EMAIL | PHONE
// # *********************************************************
// Task Distribution
// Member_1: Hew Wee Bo
// Member_2:
// Member_3:
// Member_4:
// # *********************************************************
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <chrono>
#include <iomanip>
using namespace std;
// record: integer (1B-10B) and 5-char string
struct Record {
long long num;
string str;
};
int getDigit(long long num, int digitPos) {
return (num / (long long)pow(10, digitPos)) % 10;
}
void countingSortByDigit(vector<Record>& records, int digitPos) {
int n = (int)records.size();
vector<Record> output(n);
vector<int> count(10, 0);
for (int i = 0; i < n; i++) {
int digitValue = getDigit(records[i].num, digitPos);
count[digitValue]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
int digitValue = getDigit(records[i].num, digitPos);
output[count[digitValue] - 1] = records[i];
count[digitValue]--;
}
records = output;
}
void radixSort(vector<Record>& records) {
const int totalDigits = 10;
for (int digitPos = 0; digitPos < totalDigits; digitPos++) {
countingSortByDigit(records, digitPos);
}
}
string getOutputFilename(const string& inputFilename) {
size_t pos = inputFilename.find("dataset_");
if (pos != string::npos) {
return "radix_sorted_" + inputFilename.substr(pos);
}
return "radix_sorted_" + inputFilename;
}
bool readDataset(const string& filename, vector<Record>& records) {
ifstream inputFile(filename);
if (!inputFile.is_open()) {
return false;
}
string line;
while (getline(inputFile, line)) {
size_t commaPos = line.find(',');
if (commaPos != string::npos) {
long long num = stoll(line.substr(0, commaPos));
string str = line.substr(commaPos + 1);
records.push_back({num, str});
}
}
return true;
}
bool writeSortedOutput(const string& outputFilename,
const vector<Record>& records,
const string& inputFilename,
double elapsedSeconds) {
ofstream outputFile(outputFilename);
if (!outputFile.is_open()) {
return false;
}
for (const auto& record : records) {
outputFile << record.num << "/" << record.str << endl;
}
outputFile << "Radix sort running time for " << inputFilename << ": "
<< fixed << setprecision(6) << elapsedSeconds << " seconds" << endl;
return true;
}
void processFile(const string& filename) {
vector<Record> records;
if (!readDataset(filename, records)) {
cerr << "Error: Could not open file " << filename << endl;
return;
}
auto start = chrono::high_resolution_clock::now();
radixSort(records);
auto end = chrono::high_resolution_clock::now();
chrono::duration<double> elapsed = end - start;
string outputFilename = getOutputFilename(filename);
if (!writeSortedOutput(outputFilename, records, filename, elapsed.count())) {
cerr << "Error: Could not create output file " << outputFilename << endl;
return;
}
cout << "Radix sort running time for " << filename << ": "
<< fixed << setprecision(6) << elapsed.count() << " seconds" << endl;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
cerr << "Usage: " << argv[0] << " <dataset_file1.csv> [dataset_file2.csv] ..." << endl;
return 1;
}
// process each input file
for (int i = 1; i < argc; i++) {
processFile(argv[i]);
}
return 0;
}