-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapQuick.cpp
133 lines (105 loc) · 4.99 KB
/
HeapQuick.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void heapify(vector<long long>& pinakas, int n, int i, vector<int>& day, vector<int>& month, vector<int>& year);
void heapSort(vector<long long>& pinakas, int n, vector<int>& day, vector<int>& month, vector<int>& year);
void quickSort(vector<long long>& pinakas, int start, int finish, vector<int>& day, vector<int>& month, vector<int>& year);
int partition(vector<long long>& pinakas, int start, int finish, vector<int>& day, vector<int>& month, vector<int>& year);
int main(){
ifstream file("file.csv"); //opening our file
if (!file.is_open()){
cout<<"Error opening file"<<endl;
return 1;
}
int records=0, i=0;
vector <int> day, month, year;
vector <long long> cumulative;
string line;
while(getline(file,line)){
vector<string> row;
stringstream ss(line);
string cell;
while(getline(ss, cell, ',')) //splitting lines into rows
row.push_back(cell);
stringstream ssl(row[2]); //extracting the dates from the 3rd row
string dayStr, monthStr, yearStr;
getline(ssl,dayStr,'/');
getline(ssl,monthStr,'/');
getline(ssl,yearStr,'/');
//storing dates as integers and cumulatives as long long numbers in vectors
day.push_back(stoi(dayStr));
month.push_back(stoi(monthStr));
year.push_back(stoi(yearStr));
cumulative.push_back(stoll(row[9]));
records++;
}
//letting the user choose between the 2 algorithms
int choice;
do{
cout<<"Press 1 to use Heap Sort or press 2 to use Quick Sort in order to sort cumulatives:"<<endl;
cin>>choice;
} while(choice!=1 && choice!=2);
if(choice==1)
heapSort(cumulative,records,day,month,year);
else if(choice==2)
quickSort(cumulative,0,records-1,day,month,year);
for(i=0; i<records; i++)
cout<<"Cumulative: "<<cumulative[i]<<" -> date: "<<day[i]<<"/"<<month[i]<<"/"<<year[i]<<"\t";
if(choice==1)
cout<<endl<<endl<<"Heap Sort was executed successfully";
else
cout<<endl<<endl<<"Quick Sort was executed successfully";
return 0;
}
void heapify(vector<long long>& pinakas, int n, int i, vector<int>& day, vector<int>& month, vector<int>& year){
int largest=i; //root
int left=2*i+1; //left child
int right=2*i+2; //right child
if (left<n && pinakas[left]>pinakas[largest]) //check if left child is bigger than the root and if yes set it as largest
largest=left;
if (right<n && pinakas[right]>pinakas[largest]) //check if right child is bigger than the largest and if yes set itself as largest
largest=right;
if (largest!=i){
swap(pinakas[i], pinakas[largest]); //if root is not the largest swap the two elements
heapify(pinakas, n, largest, day, month, year); //recursive call of heapify function in order to heapify the new subtree
}
}
void heapSort(vector<long long>& pinakas, int n, vector<int>& day, vector<int>& month, vector<int>& year){
for(int i=n/2-1; i>=0; i--) //start from the last non-leaf node and move up to the root
heapify(pinakas, n, i, day, month, year); //heapify
for(int i=n-1; i>=0; i--){
swap(pinakas[0], pinakas[i]);
heapify(pinakas, i, 0, day, month, year);
}
}
void quickSort(vector<long long>& pinakas, int start, int finish, vector<int>& day, vector<int>& month, vector<int>& year){
int pos;
if(start<finish){
pos=partition(pinakas, start, finish, day, month, year);
quickSort(pinakas, start, pos-1, day, month, year); //recursive call of quickSort applied on the left sub-vector
quickSort(pinakas, pos+1, finish, day, month, year); //recursive call of quickSort applied on the right sub-vector
}
}
int partition(vector<long long>& pinakas, int start, int finish, vector<int>& day, vector<int>& month, vector<int>& year){
int i,j;
long long pivot;
pivot=pinakas[start]; //randomly choosing the first element of the vector "pinakas" as the pivot element of the algorithm
i=start+1; //index that will run through the vector from left to right
j=finish; //index that will run through the vector from right to left
while(true){
while(i<=finish && pinakas[i]<=pivot) //searching for a number bigger than pivot that's on the left of it
i++;
while(j>=start && pinakas[j]>pivot) //searching for a number smaller than pivot that's on the right of it
j--;
if(i<j)
swap(pinakas[i], pinakas[j]); //if i is still on the left of j then swap the two numbers that were found from the searching above
else{
swap(pinakas[start], pinakas[j]); //else swap the "start" number with the number that j is pointing at and also return j
return j;
}
}
}