-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap_sort.cpp
More file actions
143 lines (117 loc) · 4.26 KB
/
Copy pathheap_sort.cpp
File metadata and controls
143 lines (117 loc) · 4.26 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
// # *********************************************************
// Program: heap_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 <chrono>
#include <iomanip>
using namespace std;
struct Record {
long long num;
string str;
};
void maxHeapify(vector<Record>& records, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && records[left].num > records[largest].num)
largest = left;
if (right < n && records[right].num > records[largest].num)
largest = right;
if (largest != i) {
Record temp = records[i];
records[i] = records[largest];
records[largest] = temp;
maxHeapify(records, n, largest);
}
}
// complete heap sort algorithm implementation
void heapSort(vector<Record>& records) {
int n = (int)records.size();
// 1. build max heap
for (int i = n / 2 - 1; i >= 0; i--) {
maxHeapify(records, n, i);
}
// 2. heap sort
for (int i = n - 1; i > 0; i--) {
Record temp = records[0];
records[0] = records[i];
records[i] = temp;
maxHeapify(records, i, 0);
}
}
string getOutputFilename(const string& inputFilename) {
size_t pos = inputFilename.find("dataset_");
if (pos != string::npos) {
return "heap_sort_" + inputFilename.substr(pos);
}
return "heap_sort_" + 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;
}
// Append the performance breakdown to match the style found in your radix_sort file
outputFile << "Heap 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();
heapSort(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 << "Heap 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", "dataset_10000.csv", "dataset_100000.csv"}; // default
// vector<string> inputs = {"dataset_100000.csv"};
for (const string& filename : inputs) {
processFile(filename);
}
return 0;
}