-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeSort.cpp
118 lines (107 loc) · 3.14 KB
/
mergeSort.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <chrono>
#include <iomanip>
using namespace std;
struct RatingData {
int id;
string title;
double rating;
};
void merge(RatingData*& tab, int l, int m, int r, RatingData*& temp) {
size_t tempSize = r - l + 1;
if (tempSize > 1) {
int a = l, b = m + 1, i = 0;
while (i < tempSize) {
if (a <= m && (b > r || tab[a].rating < tab[b].rating))
temp[i++] = tab[a++];
else
temp[i++] = tab[b++];
} // Porownanie dwoch stron tablicy
// Przepisanie wartosci do oryginalnej tablicy
for (size_t j = 0; j < tempSize; j++) {
tab[l + j] = temp[j];
} // l to przesuniecie
// miejsce w ktorym nalezy dodac wartosc
// skoro tablica jest jednoargumentowa
}
}
void mergeSort(RatingData*& tab, int l, int r) {
if ( l < r ) {
RatingData* temp = new RatingData[r - l + 1]; // Tymczasowa tablica
int m = static_cast<int>(floor((l + r) / 2));
mergeSort(tab, l, m); // Zmniejszenie lewej strony tablicy
mergeSort(tab, m + 1, r); // Zmniejszenie prawej strony tablicy
merge(tab, l, m, r, temp); // Scalenie i porownanie powyzszych czesci
delete[] temp;
}
}
void readFile(RatingData*& ratings, int& max) {
ifstream file("C:\\Users\\nicwr\\Downloads\\projekt2_dane.csv");
if (!file.is_open()) {
cout << "Error: Could not open file." << endl;
return;
}
const int INCREASE_VALUE = 1000000;
int MAX_RATINGS = INCREASE_VALUE;
ratings = new RatingData[INCREASE_VALUE];
string line;
max = 0;
getline(file, line);
while (getline(file, line)) {
stringstream ss(line);
RatingData data;
string id_str, rating_str;
getline(ss, id_str, ',');
data.id = stoi(id_str);
getline(ss, data.title, ',');
if (ss >> data.rating && data.rating <= 10) {
if (max >= MAX_RATINGS) {
// Dynamiczne powiększanie tablicy ratings / wykorzystanie dwukrotnej alokacji pamieci
MAX_RATINGS *= 2;
RatingData* temp = new RatingData[MAX_RATINGS];
for (int i = 0; i < max; ++i) {
temp[i] = ratings[i];
}
delete[] ratings;
ratings = temp;
}
ratings[max++] = data;
}
}
file.close();
}
int main() {
RatingData* ratings;
int max;
readFile(ratings, max);
const int sortingSize[5]{ 10000, 100000, 500000, 1000000, max };
float avr;
for (size_t i = 0; i < 5; i++) {
avr = 0.0;
for (size_t j = 0; j < 3; j++) {
int value = sortingSize[i];
if (value > max) {
value = max;
}
auto start = chrono::steady_clock::now();
mergeSort(ratings, 0, value-1);
auto end = chrono::steady_clock::now();
auto elapsed_ms = chrono::duration_cast<chrono::microseconds>(end - start);
avr += static_cast<double>(elapsed_ms.count()) / 1000000;
if (i == 0 && j == 0) {
cout << "Sorted Ratings:\n";
for (size_t k = 0; k < sortingSize[i]; k++) {
cout << "Rating: " << ratings[k].rating << ", Title: " << ratings[k].title << endl;
}
cout << endl;
}
if (j == 2) {
cout << "Ilosc: " << sortingSize[i] << " Czas: " << fixed << setprecision(4) << avr / 3 << " sekund" << endl;
}
}
}
delete[] ratings;
}