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
|
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
#include "Sorting.h"
#include "SortingHelper.h"
enum sort_t { SELECTION, INSERTION, MERGE, QUICK };
enum input_t { INCREASING, DECREASING, CONSTANT, RANDOM };
#define DEFAULT_N 1000
#define DEFAULT_ALG MERGE
#define DEFAULT_INPUT RANDOM
#define N_ARG 1
#define ALG_ARG 2
#define INPUT_ARG 3
#define MIN_ARGS 1
#define MAX_ARGS 4
#define PRINT_USAGE() cout << argv[0] << " [n] [algorithm] [input type]" << endl << endl << \
"n (optional): size of array (default: " << DEFAULT_N << ')' << endl << \
"algorithm (optional): one of selectionsort, insertionsort, mergesort, or quicksort (simq, default mergesort)" << endl << \
"input type (optional): one of increasing, decreasing, constant, or random (idcr, default random)" << endl;
int main(int argc, char** argv)
{
int n = DEFAULT_N;
sort_t alg = DEFAULT_ALG;
input_t intype = DEFAULT_INPUT;
int* data = nullptr;
int* temp;
clock_t start;
clock_t timing[3];
//Parse input arguments
if (argc < MIN_ARGS || argc > MAX_ARGS)
{
PRINT_USAGE();
return -1;
}
if (argc > N_ARG)
n = atoi(argv[N_ARG]);
temp = zeroArray(n);
if (argc > ALG_ARG)
{
switch (argv[ALG_ARG][0])
{
case 's':
case 'S':
alg = SELECTION;
break;
case 'i':
case 'I':
alg = INSERTION;
break;
case 'm':
case 'M':
alg = MERGE;
break;
case 'q':
case 'Q':
alg = QUICK;
break;
default:
cout << "Sorting algorithm not recognized\n";
}
}
if (argc > INPUT_ARG)
{
switch (argv[INPUT_ARG][0])
{
case 'i': //increasing
case 'I':
case 'a': //ascending
case 'A':
intype = INCREASING;
break;
case 'd': //decreasing, descending
case 'D':
intype = DECREASING;
break;
case 'c': //constant
case 'C':
case 'z': //zero
case 'Z':
intype = CONSTANT;
break;
case 'r': //random
case 'R':
intype = RANDOM;
break;
default:
cout << "Input array type not recognized\n";
}
}
//Run sorting algorithm 3 times
for (int i = 0; i < 3; i++)
{
// Initialize data
switch (intype)
{
case INCREASING:
data = increasingArray(n);
break;
case DECREASING:
data = decreasingArray(n);
break;
case CONSTANT:
data = zeroArray(n);
break;
case RANDOM:
data = randomArray(n);
}
//Sort data
//printArray(arr, n);
start = clock();
switch (alg)
{
case SELECTION:
selectionsort(data, n);
break;
case INSERTION:
insertionsort(data, n);
break;
case MERGE:
mergesort(data, n, temp);
break;
case QUICK:
quicksort(data, n);
}
timing[i] = clock() - start;
//printArray(arr, n);
//Verify data is sorted
if (isSorted(data, n))
cout << "Array successfully sorted.\n";
else
{
cout << "Array incorrectly sorted.\n";
//Time to debug...
return -1;
}
}
//Output timing results
for (int i = 0; i < 3; i++)
cout << "Attempt " << i + 1 << ": " << setw(8) << (int)(timing[i] * 1000.0 / CLOCKS_PER_SEC) << " ms\n";
cout << "Median time: " << setw(8) << (int)(timing[medianof3(timing[0], timing[1], timing[2]) - 1] * 1000.0 / CLOCKS_PER_SEC) << " ms" << endl;
free(data);
free(temp);
//Windows: pause so window doesn't vanish
char c;
cout << "Type 'q' to quit: ";
cin >> c;
return 0;
}
|