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.
38 #include "stdlib/wiki.c"
42 * Integer comparison function for stdlib sorting algorithms
45 sorthelp(const void *a, const void *b)
47 if (*(int *)a > *(int *)b)
49 if (*(int *)a < *(int *)b)
57 * Enumerated types for the different types of tests and sorting algorithms
59 enum test { RAND, SORT, PART, REV, INVALID_TEST };
62 enum sort { MERGE, WIKI, QUICK, HEAP, INVALID_ALG };
64 enum sort { MERGE, QUICK, HEAP, INVALID_ALG };
68 * Sort an array with the given algorithm
71 sort(int *testarray, int elts, enum sort s)
75 mergesort(testarray, (size_t)elts, sizeof(int), sorthelp);
79 WikiSort(testarray, (size_t)elts, sizeof(int), sorthelp);
83 qsort(testarray, (size_t)elts, sizeof(int), sorthelp);
86 heapsort(testarray, (size_t)elts, sizeof(int), sorthelp);
88 // Should never be reached
95 * Sort an array of randomly generated integers
98 rand_bench(int elts, enum sort s)
100 size_t size = sizeof(int) * elts;
101 int *array = malloc(size);
102 arc4random_buf(array, size);
103 sort(array, elts, s);
108 * Sort an array of increasing integers
111 sort_bench(int elts, enum sort s)
113 size_t size = sizeof(int) * elts;
114 int *array = malloc(size);
115 for (int i = 0; i < elts; i++) {
118 sort(array, elts, s);
123 * Sort an array of partially increasing, partially random integers
126 partial_bench(int elts, enum sort s)
128 size_t size = sizeof(int) * elts;
129 int *array = malloc(size);
130 for (int i = 0; i < elts; i++) {
134 array[i] = arc4random();
136 sort(array, elts, s);
141 * Sort an array of decreasing integers
144 reverse_bench(int elts, enum sort s)
146 size_t size = sizeof(int) * elts;
147 int *array = malloc(size);
148 for (int i = 0; i < elts; i++) {
151 sort(array, elts, s);
156 run_bench(enum sort s, enum test t, int runs, int elts)
158 for (int i = 0; i < runs; i++) {
167 partial_bench(elts, s);
170 reverse_bench(elts, s);
172 // Should never be reached
182 if (strcmp(alg, "merge") == 0)
185 else if (strcmp(alg, "wiki") == 0)
188 else if (strcmp(alg, "quick") == 0)
190 else if (strcmp(alg, "heap") == 0)
197 parse_test(char *test)
199 if (strcmp(test, "rand") == 0)
201 else if (strcmp(test, "sort") == 0)
203 else if (strcmp(test, "part") == 0)
205 else if (strcmp(test, "rev") == 0)
212 usage(const char *progname)
215 printf("\t%s: [alg] [test] [runs] [elt_power]\n", progname);
217 printf("Valid algs:\n");
219 printf("\theap merge quick wiki\n");
221 printf("\theap merge quick\n");
223 printf("Valid tests:\n");
224 printf("\trand sort part rev\n");
225 printf("\trand: Random element array \n");
226 printf("\tsort: Increasing order array \n");
227 printf("\tpart: Partially ordered array\n");
228 printf("\trev: Decreasing order array\n");
229 printf("Run the algorithm [runs] times with 2^[elt_power] elements\n");
234 * Runs a sorting algorithm with a provided data configuration according to
235 * command line arguments
238 main(int argc, char *argv[])
240 const char *progname = argv[0];
245 enum sort s = parse_alg(argv[1]);
246 if (s == INVALID_ALG)
249 enum test t = parse_test(argv[2]);
250 if (t == INVALID_TEST)
253 runs = atoi(argv[3]);
254 elts = pow(2, atoi(argv[4]));
256 run_bench(s, t, runs, elts);