-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort
More file actions
189 lines (166 loc) · 4.73 KB
/
QuickSort
File metadata and controls
189 lines (166 loc) · 4.73 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <queue>
using namespace std;
void quickSort(double arr[], int low, int high);
double partition(double arr[], int low, int high);
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class Movie_Vertex
{
private:
std::vector<std::pair<double, Movie_Vertex>> adjVerts;
std::string title, genre;
double rating, price;
public:
Movie_Vertex(std::string title, std::string genre, double rating);
void addEdge(Movie_Vertex& adjVert);
void removeEdge(Movie_Vertex& adjVert);
std::vector<std::pair<double, Movie_Vertex>> getEdges();
std::string getTitle();
std::string getGenre();
double getRating();
double getPrice();
};
// Constructor for Movie_Vertex, stores all information relating to the movie dataset
Movie_Vertex::Movie_Vertex(string title, string genre, double rating)
{
this->title = title;
this->genre = genre;
this->rating = rating;
}
//Adds adjVert to a list of connected verticies, adjVerts
void Movie_Vertex::addEdge(Movie_Vertex& adjVert)
{
//Prevent duplicates
for(int i = 0; i < adjVerts.size(); i++)
{
if(adjVerts[i].second.getTitle().compare(adjVert.getTitle()) == 0)
{
return;
}
}
//Edge weight determined by sum of ratings
//Higher ratings will have lower weights
//Maximum rating is 10+10 -> weight of 0.05
adjVerts.push_back(make_pair(1/(getRating()+adjVert.getRating()), adjVert));
}
// Remove the connection between this vertex and adjVert
void Movie_Vertex::removeEdge(Movie_Vertex& adjVert)
{
for (int i = 0; i < adjVerts.size(); i++)
{
if (adjVerts[i].second.getTitle() == adjVert.getTitle())
{
adjVerts.erase(adjVerts.begin() + i);
}
}
}
// Return all connections of this vertex
vector<pair<double, Movie_Vertex>> Movie_Vertex::getEdges()
{
return adjVerts;
}
string Movie_Vertex::getTitle()
{
return title;
}
string Movie_Vertex::getGenre()
{
return genre;
}
double Movie_Vertex::getPrice()
{
return price;
}
double Movie_Vertex::getRating()
{
return rating;
}
void displayMovie(Movie_Vertex movie) {
cout << "Movie Selected:" << endl;
cout << "Title: " << movie.getTitle() << endl;
cout << "Genre: " << movie.getGenre() << endl;
cout << "Rating: " << movie.getRating() << endl;
cout << "Price: " << movie.getPrice() << endl;
}
int main() {
queue<double> q;
vector<Movie_Vertex> vecOfMovies;
Movie_Vertex a("A", "Horror",4.19);
Movie_Vertex b("B", "Comedy",5.0);
Movie_Vertex c("C", "Action",4.11);
Movie_Vertex d("D", "Documentary",4.79);
Movie_Vertex e("E", "Comedy",4.37);
Movie_Vertex f("F", "Short",4.83);
Movie_Vertex g("G", "Horror",4.17);
Movie_Vertex h("H", "Action",4.44);
Movie_Vertex i("I", "Documentary",4.26);
Movie_Vertex j("J", "Horror",4.01);
vecOfMovies.push_back(a);
vecOfMovies.push_back(b);
vecOfMovies.push_back(c);
vecOfMovies.push_back(d);
vecOfMovies.push_back(e);
vecOfMovies.push_back(f);
vecOfMovies.push_back(g);
vecOfMovies.push_back(h);
vecOfMovies.push_back(i);
vecOfMovies.push_back(j);
//RATING
double arr[vecOfMovies.size()];
int originalSize = vecOfMovies.size();
for(int i = 0; i < originalSize; i++){
arr[i] = vecOfMovies.at(i).getRating();
//could do getYear if added or getPrice if parameter added
}
int m = sizeof(arr)/sizeof(arr[0]);
int low = 0;
int high = m-1;
quickSort(arr, low, high);
cout << "organized"<< endl;
vector<Movie_Vertex> sortedVector;
for(int i = 0; i < originalSize; i++){
for(int j = 0; j < originalSize; j++) {
if (arr[i] == vecOfMovies.at(j).getRating()){
sortedVector.push_back(vecOfMovies.at(j));
}
}
}
for(int k = 0; k < sortedVector.size(); k++){
cout << k + 1 << ".";
cout << sortedVector.at(k).getTitle() << endl;
}
return 0;
}
void quickSort(double arr[], int low, int high) {
if(low < high){
double pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1.0);
quickSort(arr, pivot + 1.0, high);
}
}
double partition(double arr[], int low, int high){
double pivot = arr[low];
int up = low;
int down = high;
while(up < down){
for(int i = up; i < high; i++){
if(arr[up] > pivot){
break;
}
up += 1;
for(int j = high; j > low; j--){
if(arr[down] < pivot){
break;
}
down -= 1;
}
if(up < down) {
swap(arr[up], arr[down]);
}
}
}
swap(arr[low],arr[down]);
return down;