-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix_sort.cpp
More file actions
140 lines (119 loc) · 4.64 KB
/
Copy pathradix_sort.cpp
File metadata and controls
140 lines (119 loc) · 4.64 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
// # *********************************************************
// Program: radix_sort.cpp
// Course: CCP6214 Algorithm Design and Analysis
// Lecture Class: TC2L
// Tutorial Class: TT5L
// Trimester: 2610
// Member_1: HEW WEE BO | hew.wee.bo@student.mmu.edu.my | 0128803121
// Member_2: ID | JEVAANRAJ A/L RAJA KUMARAN | jevaanraj.raja.kumaran@student.mmu.edu.my | 0179651973
// Member_3: ID | SHANJIF CAKRAVRTHI A/L KUPPAN @ SIVA KUMAR | shanjif.cakravthi@student.mmu.edu.my | 0195601010
// Member_4: ID | TEH ZHAO JIN | teh.zhao.jin@student.mmu.edu.my | 01111279290
// # *********************************************************
// Task Distribution
// Member_1: Hew Wee Bo
// Member_2: Jevaanraj
// Member_3: Shanjif
// Member_4: Teh Zhao Jin
// # *********************************************************
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <chrono>
#include <iomanip>
using namespace std;
// simple record: a 10-digit integer and its 5-char label
struct Record {
long long num;
string str;
};
// return the digit at position digitPos (0 = rightmost)
int getDigit(long long num, int digitPos) {
return (num / (long long)pow(10, digitPos)) % 10;
}
// stable counting sort on a single digit (used by LSD radix)
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]++;
}
// cumulative counts
for (int i = 1; i < 10; i++) count[i] += count[i - 1];
// place items into output array in stable order
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;
}
// perform LSD radix sort (10 passes, right-to-left)
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;
}
// read entire CSV into memory (simple parser, no header expected)
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;
}
// write sorted records and append timing info
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;
}
// read -> sort (timed) -> write
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() {
// choose input files by commenting/uncommenting the options below
// vector<string> inputs = {"dataset_1000.csv"};
vector<string> inputs = {"dataset_1000.csv", "dataset_10000.csv", "dataset_100000.csv"}; // default
// vector<string> inputs = {"dataset_100000.csv"};
for (const auto& f : inputs) processFile(f);
return 0;
}