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
|
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
const int BIGSIZE = 64000;
int bigArray[BIGSIZE];
const int testSizes[] = {1000, 2000, 4000, 8000, 16000, 32000, BIGSIZE, 0}; // 0 is a sentinel
// All numbers inside testSizes MUST BE less than or equal to BIGSIZE
const int TARGET = 123456789; // TARGET is uses as a number to search for.
// array helper functions:
void showArray(int array[], int size, const string &msg="") { // displays every element in array
cout<<msg;
for (int i{}; i<size; ++i)
cout << setw(2) << array[i]; // assumes small numbers
cout << endl;
}
// Useful to verify that array is really sorted!
bool verifySorted(int array[], int size) {
// returns true if array is in ascending order, else false.
for (int i=0; i<(size-1); ++i)
if (array[i]>array[i+1]) return false;
return true;
}
int unorderedCount(int array[], int size) {
int unsorted_adjacent_pairs{};
for (int i=0; i<(size-1); ++i)
if (array[i]>array[i+1]) ++unsorted_adjacent_pairs;
return unsorted_adjacent_pairs;
}
const int AlgorithmNameMaxWidth=16; // maximum column width to display algorithm names
const int TestSizeWidth=12; // maximum column width to display time to run algorithms
const int DurationWidth=TestSizeWidth;
const string SELECTION_SORT_NAME{"selection sort"};
void selectionSortConcise(int[], int); // from internet, not called
void selectionSortTextBook(int[], int); // from our textbook, not called
void selectionSort(int[], int, bool); // as expected for this lab
const string SORT1_NAME {"my sort 1"};
void mySort1(int[], int, bool); // expected for this lab <change code>
const string SORT2_NAME {"my sort 2"};
void mySort2(int[], int, bool); // expected for this lab <change code>
bool linearSearchArray(int [], int, int, int&); // provided
bool binarySearchArray(int [], int, int, int&); // expected for this lab <change code>
float testSortAlgorithm1x(void sortAlgorithm(int [], int, bool),
int array[], int arraySize, bool verbose=false) {
for (int index = 0; index < arraySize; index++)
array[index] = rand(); // initialize array with random values
int startTime = clock(); // get the start time, in milliseconds
sortAlgorithm(array, arraySize, verbose); // ALGORITHM UNDER TEST
int stopTime = clock(); // get the stop time, in milliseconds
float duration = stopTime - startTime;
int unordered_pairs = unorderedCount(array, arraySize);
if (unordered_pairs != 0)
return -unordered_pairs; // return a negative count of unordered pairs to indicate sort failure
else
return duration;
}
void testSortAlgorithmNx(void sortAlgorithm(int [], int, bool), string sortName,
int array[], int arraySize, bool verbose=false) {
cout << endl << setw (AlgorithmNameMaxWidth) << left << sortName;
for (int testCount=0; (testSizes[testCount] && testSizes[testCount] <= arraySize); ++testCount)
cout << setw(DurationWidth) << right << testSortAlgorithm1x(sortAlgorithm, array, testSizes[testCount]);
}
float testLinearSearch(int array[], int arraySize, int retry=1000) {
bool found = false; // true if TARGET is found in array
int foundAt = -1; // index in array where TARGET was found
int startTime = clock();
for (int repeat = 0; repeat < retry; ++repeat) // repeat test 1000 times to increase duration
found = linearSearchArray(array, arraySize, TARGET, foundAt); // ALGORITHM UNDER TEST
int stopTime = clock();
float duration = stopTime - startTime;
return duration/retry; // divide duration by 1000 to get time for single search
}
// <add code> to test binary search. It works like testLinearSearch
float testBinarySearch(int array[], int arraySize, int retry=1000) {
return 0.0;
}
void testAlgorithms(int array[], int arraySize, bool verbose=false) {
cout << setw (AlgorithmNameMaxWidth) << left << "Algorithm";
for (int testCount=0; testSizes[testCount]; ++testCount)
cout << setw(TestSizeWidth) << right << testSizes[testCount];
cout << endl << string(AlgorithmNameMaxWidth, '=');
for (int testCount=0; testSizes[testCount]; ++testCount)
cout << setw(DurationWidth) << right << " =======";
testSortAlgorithmNx(selectionSort, SELECTION_SORT_NAME, array, arraySize); // provided
testSortAlgorithmNx(mySort1, SORT1_NAME, array, arraySize);
testSortAlgorithmNx(mySort2, SORT2_NAME, array, arraySize);
cout<<"\n--------"; // separator between sort algorithms and search algorithms
cout << endl << setw (AlgorithmNameMaxWidth) << left << "linear search";
for (int testCount=0; (testSizes[testCount] && testSizes[testCount] <= arraySize); ++testCount)
cout << setw(DurationWidth) << right << testLinearSearch(array, testSizes[testCount]);
cout << endl << setw (AlgorithmNameMaxWidth) << left << "binary search";
for (int testCount=0; (testSizes[testCount] && testSizes[testCount] <= arraySize); ++testCount)
cout << setw(DurationWidth) << right << testBinarySearch(array, testSizes[testCount]);
cout << endl;
}
void testSortOnSmallArray(void sortAlgorithm(int [], int, bool), string sortName) {
int smallArray[] {7, 9, 3, 1, 8, 6, 2}; // for testing purposes
const int SMALLSIZE = sizeof(smallArray)/sizeof(smallArray[0]);
showArray(smallArray, SMALLSIZE, sortName + " start: smallArray is: ");
sortAlgorithm(smallArray, SMALLSIZE, true); // true means verbose, show details
showArray(smallArray, SMALLSIZE, sortName + " stop: smallArray is: ");
cout << ((verifySorted(smallArray, SMALLSIZE)) ?
"Verified: smallArray is sorted.\n\n" :
"Oops!!!: smallArray is NOT sorted.\n\n");
}
int main () {
srand(time(0)); // seed the random number generator only once.
cout << "Test sorting algorithms on small array:\n\n";
testSortOnSmallArray(selectionSort, SELECTION_SORT_NAME);
testSortOnSmallArray(mySort1, SORT1_NAME);
testSortOnSmallArray(mySort2, SORT2_NAME);
float duration = 0.0; // time in milliseconds
duration = testSortAlgorithm1x(selectionSort, bigArray, BIGSIZE);
cout << fixed << setprecision(2);
cout << "\nSelection sort on bigArray took: "
<< setw(7) << duration << " milliseconds." << endl;
duration = testLinearSearch(bigArray, BIGSIZE);
cout << "Linear search of bigArray took: "
<< setw(7) << duration << " milliseconds.\n\n";
testAlgorithms(bigArray, BIGSIZE);
return 0;
}
// end of main
|