-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabSorting-main.cpp
More file actions
143 lines (116 loc) · 4.26 KB
/
labSorting-main.cpp
File metadata and controls
143 lines (116 loc) · 4.26 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
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <iostream>
#include <new> // std::bad_alloc
#include <vector>
#include <string>
#include <algorithm> // std::sort
#include <cstring> // memcpy
using std::cout;
using std::cerr;
using std::endl;
using std::cin;
using std::vector;
using std::string;
#include "insertionSort.h"
#include "bubbleSort.h"
#include "heapsort.h"
#include "mergesort.h"
#include "quicksort.h"
// callback function that takes arguments of an int* and an int. For example, insertionSort(int* array, int count)
typedef void (*SortingFunction)(int*, int);
// Populate array with random values (using the same seed each time)
void setRandomValues(int array[], int size, time_t seed = time(NULL)){
/* initialize random seed: */
srand( (unsigned int) seed); // seed the random number generator
// cerr << "Unsorted array:\n";
for( int i = 0; i < size; i++){
array[i] = rand() % size;
// cerr << array[i] << " ";
}
// cerr << endl;
}
// use a library sorting algorithm to sort array
void setAnswerKey(int array[], int size){
std::sort( array, array + size);
}
// compare the values in array and answers
void verifySort(int array[], int answers[], int size){
for( int i = 0; i < size; i++){
if( array[i] != answers[i]){
cerr << "ERROR: Array not sorted at index " << i << ": (" << array[i] << " should've been " << answers[i] << ")!\n";
exit(1);
}
}
}
int main( int argc, char* argv[]){
int maxN = -1; // largest array size
int* arrayOrig; // array of random elements
int* array; // array of random elements (copied from arrayOrig)
int* answers; // sorted array (used for verification)
time_t seed; // seed for random number generator
vector< string> sortingFunctionNames;
vector< SortingFunction > sortingFunctionCallbacks;
sortingFunctionNames.push_back( "Insert");
sortingFunctionNames.push_back( "Bubble");
sortingFunctionNames.push_back( "Heap");
sortingFunctionNames.push_back( "Merge");
sortingFunctionNames.push_back( "Quick");
sortingFunctionCallbacks.push_back( insertionSort);
sortingFunctionCallbacks.push_back( bubbleSort);
sortingFunctionCallbacks.push_back( heapsort);
sortingFunctionCallbacks.push_back( mergesort);
sortingFunctionCallbacks.push_back( quicksort);
// check for command-line argument(s)
if( argc >= 2){
// use command-line value as the seed
seed = atoi( argv[1]);
}else{
// default to the current time
seed = time(NULL);
}
do{
cout << "Please specify the largest array size: ";
cin >> maxN;
}while( ! cin );
cout << endl;
cout << "n";
for( unsigned int sortI = 0; sortI < sortingFunctionNames.size(); sortI++){
cout << "\t" << sortingFunctionNames[ sortI];
}
cout << endl;
// use different values of n, starting at 100, and growing by 10x until the user specified limit is reached
for(int n = 100; n <= maxN; n *= 10){
try{
// allocate memory
arrayOrig = new int[ n];
array = new int[ n];
answers = new int[ n];
} catch (std::bad_alloc& ba) {
cerr << "Memory allocation error: " << ba.what() << '\n';
exit(1);
}
// populate the array with random values
setRandomValues(arrayOrig, n, seed);
//
// Sort answers
//
// copy contents of arrayOrig into array
memcpy( answers, arrayOrig, n * sizeof( int));
setAnswerKey(answers, n);
cout << n;
for( unsigned int sortI = 0; sortI < sortingFunctionCallbacks.size(); sortI++){
// copy contents of arrayOrig into array
memcpy( array, arrayOrig, n * sizeof( int));
// call the sorting function (using a function pointer)
sortingFunctionCallbacks[ sortI]( array, n); // for example, will call insertionSort(array, n)
// verify that all of the values are sorted
verifySort( array, answers, n);
}
cout << endl;
// clean up memory
delete[] arrayOrig;
delete[] array;
}
return 0;
}