2 * Copyright (c) 2017 Miles Fertel
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13 * redistribution must be conditioned upon including a substantially
14 * similar Disclaimer requirement for further binary redistribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 * THE POSSIBILITY OF SUCH DAMAGES.
40 #include "stdlib/wiki.c"
44 * Integer comparison function for stdlib sorting algorithms
47 sorthelp(const void *a, const void *b)
49 if (*(int *)a > *(int *)b)
51 if (*(int *)a < *(int *)b)
59 * Enumerated types for the different types of tests and sorting algorithms
61 enum test { RAND, SORT, PART, REV, INVALID_TEST };
64 enum sort { MERGE, WIKI, QUICK, HEAP, INVALID_ALG };
66 enum sort { MERGE, QUICK, HEAP, INVALID_ALG };
70 * Sort an array with the given algorithm
73 sort(int *testarray, int elts, enum sort s)
77 mergesort(testarray, (size_t)elts, sizeof(int), sorthelp);
81 WikiSort(testarray, (size_t)elts, sizeof(int), sorthelp);
85 qsort(testarray, (size_t)elts, sizeof(int), sorthelp);
88 heapsort(testarray, (size_t)elts, sizeof(int), sorthelp);
90 // Should never be reached
97 * Sort an array of randomly generated integers
100 rand_bench(int elts, enum sort s)
102 size_t size = sizeof(int) * elts;
103 int *array = malloc(size);
104 arc4random_buf(array, size);
105 sort(array, elts, s);
110 * Sort an array of increasing integers
113 sort_bench(int elts, enum sort s)
115 size_t size = sizeof(int) * elts;
116 int *array = malloc(size);
117 for (int i = 0; i < elts; i++) {
120 sort(array, elts, s);
125 * Sort an array of partially increasing, partially random integers
128 partial_bench(int elts, enum sort s)
130 size_t size = sizeof(int) * elts;
131 int *array = malloc(size);
132 for (int i = 0; i < elts; i++) {
136 array[i] = arc4random();
138 sort(array, elts, s);
143 * Sort an array of decreasing integers
146 reverse_bench(int elts, enum sort s)
148 size_t size = sizeof(int) * elts;
149 int *array = malloc(size);
150 for (int i = 0; i < elts; i++) {
153 sort(array, elts, s);
158 run_bench(enum sort s, enum test t, int runs, int elts)
160 for (int i = 0; i < runs; i++) {
169 partial_bench(elts, s);
172 reverse_bench(elts, s);
174 // Should never be reached
184 if (strcmp(alg, "merge") == 0)
187 else if (strcmp(alg, "wiki") == 0)
190 else if (strcmp(alg, "quick") == 0)
192 else if (strcmp(alg, "heap") == 0)
199 parse_test(char *test)
201 if (strcmp(test, "rand") == 0)
203 else if (strcmp(test, "sort") == 0)
205 else if (strcmp(test, "part") == 0)
207 else if (strcmp(test, "rev") == 0)
214 usage(const char *progname)
217 printf("\t%s: [alg] [test] [runs] [elt_power]\n", progname);
219 printf("Valid algs:\n");
221 printf("\theap merge quick wiki\n");
223 printf("\theap merge quick\n");
225 printf("Valid tests:\n");
226 printf("\trand sort part rev\n");
227 printf("\trand: Random element array \n");
228 printf("\tsort: Increasing order array \n");
229 printf("\tpart: Partially ordered array\n");
230 printf("\trev: Decreasing order array\n");
231 printf("Run the algorithm [runs] times with 2^[elt_power] elements\n");
236 * Runs a sorting algorithm with a provided data configuration according to
237 * command line arguments
240 main(int argc, char *argv[])
242 const char *progname = argv[0];
247 enum sort s = parse_alg(argv[1]);
248 if (s == INVALID_ALG)
251 enum test t = parse_test(argv[2]);
252 if (t == INVALID_TEST)
255 runs = atoi(argv[3]);
256 elts = pow(2, atoi(argv[4]));
258 run_bench(s, t, runs, elts);