-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSortAndSearch.cpp
154 lines (137 loc) · 4.76 KB
/
SortAndSearch.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//SortAndSearch.cpp
#include "SortAndSearch.h"
#include "University.h" // Include the University.h header file
#include <algorithm>
#include <chrono>
#include <iostream>
#include "UserInterface.h"
#include "DataSeeder.h"
#include "RegisteredUser.h"
#include <string>
#include <vector>
#include <unordered_map>
UserManager userManager; // Create a UserManager object
UserInterface ui2(userManager);
// Add the declaration of displayAllUniversities function
void bubbleSortByName() {
std::vector<University>& universityData = DataSeeder::getUniversities();
int n = universityData.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (universityData[j].name > universityData[j + 1].name) {
std::swap(universityData[j], universityData[j + 1]);
}
}
}
}
void insertionSortByName() {
std::vector<University>& universityData = DataSeeder::getUniversities();
int n = universityData.size();
for (int i = 1; i < n; i++) {
University key = universityData[i];
int j = i - 1;
while (j >= 0 && universityData[j].name > key.name) {
universityData[j + 1] = universityData[j];
j = j - 1;
}
universityData[j + 1] = key;
}
}
void insertionSortByRank() {
std::vector<University>& universityData = DataSeeder::getUniversities();
int n = universityData.size();
for (int i = 1; i < n; i++) {
University key = universityData[i];
int j = i - 1;
while (j >= 0 && universityData[j].rank > key.rank) {
universityData[j + 1] = universityData[j];
j = j - 1;
}
universityData[j + 1] = key;
}
}
void bubbleSortByRank() {
std::vector<University>& universityData = DataSeeder::getUniversities();
int n = universityData.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (universityData[j].rank > universityData[j + 1].rank) {
std::swap(universityData[j], universityData[j + 1]);
}
}
}
}
void SortMenu()
{
std::cout << "\n\n";
std::cout << "======================\n";
std::cout << "===== Sort Menu =====\n";
std::cout << "======================\n";
std::cout << "\n";
std::cout << " 1. Sort universities By Rank (Insertion Sort)\n";
std::cout << " 2. Sort universities By Rank (Bubble Sort)\n";
std::cout << " 3. Sort universities By Name (Insertion Sort)\n";
std::cout << " 4. Sort universities By Name (Bubble Sort)\n";
std::cout << " 0. Main Menu\n";
std::cout << "\n";
std::cout << "==================\n";
std::cout << "Enter your choice: ";
while (true)
{
int choice;
std::cin >> choice;
switch (choice)
{
case 1:
{
auto start = std::chrono::steady_clock::now();
insertionSortByRank();
auto end = std::chrono::steady_clock::now();
displayAllUniversities();
std::chrono::duration<double> diff = end - start;
std::cout << "Sorting time (Insertion Sort by Rank): " << diff.count() << " seconds" << std::endl;
SortMenu();
}
break;
case 2:
{
auto start = std::chrono::steady_clock::now();
bubbleSortByRank();
auto end = std::chrono::steady_clock::now();
displayAllUniversities();
std::chrono::duration<double> diff = end - start;
std::cout << "Sorting time (Bubble Sort by Rank): " << diff.count() << " seconds" << std::endl;
SortMenu();
}
break;
case 3:
{
auto start = std::chrono::steady_clock::now();
insertionSortByName();
auto end = std::chrono::steady_clock::now();
displayAllUniversities();
std::chrono::duration<double> diff = end - start;
std::cout << "Sorting time (Insertion Sort by Name): " << diff.count() << " seconds" << std::endl;
SortMenu();
}
break;
case 4:
{
auto start = std::chrono::steady_clock::now();
bubbleSortByName();
auto end = std::chrono::steady_clock::now();
displayAllUniversities();
std::chrono::duration<double> diff = end - start;
std::cout << "Sorting time (Bubble Sort by Name): " << diff.count() << " seconds" << std::endl;
SortMenu();
}
break;
case 0:
return;
default:
std::cout << "Invalid option. Please try again.\n";
}
}
SearchMenu(); // Recursive call to display the search menu again
return; // Exit the function after the recursive call
}