-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
190 lines (161 loc) · 4.93 KB
/
main.cpp
File metadata and controls
190 lines (161 loc) · 4.93 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
190
#include <iostream>
#include <cstdlib>
#include <chrono>
#include "Sorting.h"
using namespace std;
class Timer
{
private:
std::chrono::time_point<std::chrono::high_resolution_clock> startTheTimer;
public:
Timer()
{
startTheTimer = std::chrono::high_resolution_clock::now();
}
~Timer()
{
Stop();
}
void Stop()
{
auto stopTheTimer = std::chrono::high_resolution_clock::now();
auto start = std::chrono::time_point_cast<std::chrono::microseconds>(startTheTimer).time_since_epoch().count();
auto end = std::chrono::time_point_cast<std::chrono::microseconds>(stopTheTimer).time_since_epoch().count();
auto duration = end - start;
double milliseconds = duration * 0.001;
std::cout << "Duration in microseconds is " << duration <<" us " << endl;
std::cout << "Duration in milliseconds is " << milliseconds <<" ms " << endl;
}
};
void PrintArray(int* inputArray, int arraySize)
{
for(int i = 0; i < arraySize; i++)
{
cout << "[" << i << "]" << " is " << inputArray[i] << endl;
}
}
int * StartSorting(Sorting algo, int selectedAlgorithm, int* inputArray, int size)
{
int *returnedArray;
switch (selectedAlgorithm) {
case 1:
{
cout << "I am running the Bubble sort algo " << endl;
Timer timer;
//execute
returnedArray = algo.BubbleSorting();
break;
}
case 2:
{
cout << "I am running the Selection sort algo " << endl;
Timer timer;
//execute
returnedArray = algo.SelectionSorting();
break;
}
case 3:
{
cout << "I am running the Insertion sort algo " << endl;
Timer timer;
returnedArray = algo.InsertionSorting();
break;
}
case 4:
{
cout << "I am running the Merge sort algo " << endl;
Timer timer;
returnedArray = algo.MergeSorting(inputArray, 0, size - 1);
break;
}
case 5:
{
cout << "I am running the Quick sort algo " << endl;
Timer timer;
returnedArray = algo.QuickSorting(inputArray, 0, size - 1);
break;
}
case 6:
{
cout << "I am running the Counting sort algo " << endl;
Timer timer;
returnedArray = algo.CountingSorting();
break;
}
case -1:
default:
break;
}
return returnedArray;
}
int main(int argc, char** argv) {
// variables
int rnd = 0;
int ctn = 0;
int * returnedArray;
int sel;
cout << "Enter the number of random integer: " << endl;
cin >> rnd;
// check if random is a positive number
if(rnd <= 0)
{
throw invalid_argument("The value of the random integer should be greater then 0");
}
try {
//get the array with the size with need
int inputArray[rnd];
//fill the array
while (rnd--) {
inputArray[ctn] = rand() % INT16_MAX + 1;
ctn++;
}
//load complete
cout << "Load of the random array is complete" << endl;
//get the size of the array
int size = *(&inputArray + 1) - inputArray;
cout << "Before the sorting" << endl;
PrintArray(inputArray, size);
Sorting algo = Sorting(inputArray, size);
cout << "" << endl;
cout << "select the algorithm" << endl;
cout << "" << endl;
cout << "Press 1 for bubble sort" << endl;
cout << "Press 2 for selection sort " << endl;
cout << "Press 3 for insertion sort " << endl;
cout << "Press 4 for merge sort " << endl;
cout << "Press 5 for quick sort " << endl;
cout << "Press 6 for continuing sort " << endl;
cout << " " << endl;
cout << "Press 7 for benchmark " << endl;
cout << " " << endl;
//-1 to exit the program
cout << "Press -1 to exit" << endl;
cin >> sel;
//run single algo
if((sel != -1) && (sel != 7))
{
returnedArray = StartSorting(algo, sel, inputArray, size);
//print the array
PrintArray(returnedArray, size);
}
//run the benchmark
if(sel == 7)
{
for(int i = 1; i <= 6; ++i)
{
//copy the same unsorted array
int * temp = inputArray;
//create the algo obj each time
Sorting sorting = Sorting(temp, size);
//timer start each time inside this function so no worries
returnedArray = StartSorting(sorting, i, temp, size);
cout << "\t===\t===\t===" << endl;
}
}
}
catch(const std::exception& e)
{
cout << "Main throws exception " << e.what() << endl;
}
return 0;
}