-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix_sort_step.cpp
More file actions
158 lines (130 loc) · 4.21 KB
/
Copy pathradix_sort_step.cpp
File metadata and controls
158 lines (130 loc) · 4.21 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
// # *********************************************************
// Program: radix_sort_step.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 <sstream>
#include <cmath>
using namespace std;
struct Record {
long long num;
string str;
};
string formatRecords(const vector<Record>& records) {
stringstream ss;
ss << "[";
for (int i = 0; i < (int)records.size(); i++) {
if (i > 0) ss << ", ";
ss << records[i].num << "/" << records[i].str;
}
ss << "]";
return ss.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;
}
int getDatasetSize(const string& filename) {
size_t pos = filename.find("dataset_");
if (pos != string::npos) {
size_t endPos = filename.find(".csv", pos);
if (endPos != string::npos) {
string sizeStr = filename.substr(pos + 8, endPos - (pos + 8));
return stoi(sizeStr);
}
}
return 0;
}
bool readRowRange(const string& filename, int startRow, int endRow, vector<Record>& records) {
ifstream inputFile(filename);
if (!inputFile.is_open()) {
return false;
}
string line;
int currentRow = 0;
while (getline(inputFile, line)) {
currentRow++;
if (currentRow < startRow) continue;
if (currentRow > endRow) break;
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;
}
string getStepOutputFilename(const string& inputFilename, int startRow, int endRow) {
int datasetSize = getDatasetSize(inputFilename);
stringstream outputSS;
outputSS << "dataset_" << datasetSize << "_radix_sorted_step_"
<< startRow << "_" << endRow << ".txt";
return outputSS.str();
}
int main(int argc, char* argv[]) {
if (argc != 4) {
cerr << "Usage: " << argv[0] << " <dataset_file.csv> <start_row> <end_row>" << endl;
return 1;
}
string filename = argv[1];
int startRow = stoi(argv[2]);
int endRow = stoi(argv[3]);
vector<Record> records;
if (!readRowRange(filename, startRow, endRow, records)) {
cerr << "Error: Could not open file " << filename << endl;
return 1;
}
if (records.empty()) {
cerr << "Error: start_row or end_row out of range for file " << filename << endl;
return 1;
}
string outputFilename = getStepOutputFilename(filename, startRow, endRow);
ofstream outputFile(outputFilename);
if (!outputFile.is_open()) {
cerr << "Error: Could not create output file " << outputFilename << endl;
return 1;
}
outputFile << formatRecords(records) << " original" << endl;
const int totalDigits = 10;
for (int digitPos = 0; digitPos < totalDigits; digitPos++) {
countingSortByDigit(records, digitPos);
int d = 10 - digitPos;
outputFile << formatRecords(records) << " d=" << d << endl;
}
outputFile.close();
return 0;
}