-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlab_sorting.cpp
More file actions
145 lines (110 loc) · 3.55 KB
/
Copy pathlab_sorting.cpp
File metadata and controls
145 lines (110 loc) · 3.55 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
#include <iomanip>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <array>
#include <sstream>
#include <set>
using namespace std;
vector<int> bubble_sort( vector<int> sequence )
{
// COMPLETE ME
return sequence;
}
vector<int> selection_sort( vector<int> sequence )
{
// COMPLETE ME
return sequence;
}
vector<int> quick_sort( vector<int> sequence )
{
// COMPLETE ME
return sequence;
}
void _quick_sort_inplace( vector<int>& sequence, int start, int end )
{
// COMPLETE ME
}
vector<int> quick_sort_inplace( vector<int> sequence )
{
_quick_sort_inplace( sequence, 0, sequence.size()-1 );
return sequence;
}
vector<int> merge_sort( vector<int> sequence )
{
// COMPLETE ME
return sequence;
}
template<typename T>
string vec_to_str( vector<T>& sequence )
{
stringstream ss;
for( T i : sequence )
ss << i << ", ";
return ss.str();
}
int main()
{
struct Test
{
string name;
vector<int>(*function)(vector<int>);
bool success;
};
vector< Test > sortingAlgorithms {
{"bubblesort", &bubble_sort, false },
{"selection sort", &selection_sort, false },
{"quicksort", &quick_sort, false },
{"quick inplace", &quick_sort_inplace, false },
{"merge sort", &merge_sort, false }};
/* ==============================================
This is the small collection of numbers test
============================================== */
// generate 10 random numbers between -100 and 100 so we can see that it's working
vector<int> smallNumbers(10), smallCorrect;
generate( smallNumbers.begin(), smallNumbers.end(), []{ return rand()%200-100; } );
// print out the small numbers
cout << "SMALL SEQUENCE TEST" << endl;
cout << setw(16) << "starting numbers: " << vec_to_str( smallNumbers ) << endl;
// sort the small numbers
smallCorrect = smallNumbers;
sort( smallCorrect.begin(), smallCorrect.end() );
cout << setw(16) << "correctly sorted: " << vec_to_str( smallCorrect ) << endl;
for( Test &t : sortingAlgorithms )
{
// run our test numbers through the sorting algorithm and save the results
vector<int>result = t.function( smallNumbers );
// print the results
cout << setw(16) << t.name << ": " << vec_to_str( result ) << endl;
// record if the test was a successs
t.success = result == smallCorrect;
}
// print which tests passed and which failed
for( Test &t : sortingAlgorithms )
{
cout << t.name << ( t.success ? " worked" : " failed" ) << endl;
}
cout << endl;
/* ==============================================
This is the big collection of numbers test
============================================== */
// generate 10000 random numbers so we can profile our code
vector<int> bigNumbers(10000), bigCorrect;
generate( bigNumbers.begin(), bigNumbers.end(), rand );
cout << "BIG SEQUENCE TEST" << endl;
// sort the big numbers
bigCorrect = bigNumbers;
sort( bigCorrect.begin(), bigCorrect.end() );
for( Test &t : sortingAlgorithms )
{
if( !t.success )
continue;
vector<int> result = t.function( bigNumbers );
t.success = result == bigCorrect;
}
for( Test &t : sortingAlgorithms )
{
cout << t.name << ( t.success ? " worked" : " failed" ) << endl;
}
return count_if( sortingAlgorithms.begin(), sortingAlgorithms.end(), [](Test &t){ return !t.success; } );
}