Skip to content
Permalink
Browse files

Added my own generic quicksort

Changed all index variables/parameters to size_t
other fixes/changes
Lots still to do
  • Loading branch information
rswinkle committed Sep 26, 2015
1 parent 48d1fdf commit 8c8c1f2d980dfe069b0dcc2350ca0ecb4b0f46ac
Showing with 266 additions and 116 deletions.
  1. +63 −36 main.c
  2. +79 −32 main.cpp
  3. +7 −3 plotbarchart.py
  4. BIN quicksort_times.png
  5. +117 −45 sorting_algs.h
99 main.c
@@ -13,31 +13,32 @@ and implementations.
#include <math.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include "sorting_algs.h"






typedef void (*sort_func)(int[], int n);
typedef void (*sort_func)(int[], size_t n);

void mergesort_wrapper(int a[], int n)
void mergesort_wrapper(int a[], size_t n)
{
mergesort(a, 0, n-1);
}

void qsort_wrapper(int a[], int n)
void qsort_wrapper(int a[], size_t n)
{
quicksort(a, 0, n-1);
}

void iter_qsort_wrapper(int a[], int n)
void iter_qsort_wrapper(int a[], size_t n)
{
iter_quicksort(a, 0, n-1);
}

void qsort_insertion_wrapper(int a[], int n)
void qsort_insertion_wrapper(int a[], size_t n)
{
quick_insertionsort(a, 0, n-1);
}
@@ -55,22 +56,26 @@ int cmp_int_lt(const void* a, const void* b)
return 0;
}

void qsort_lib_wrapper(int a[], int n)
void qsort_lib_wrapper(int a[], size_t n)
{
qsort(a, n, sizeof(int), cmp_int_lt);
}

void qsort_my_wrapper(int a[], size_t n)
{
generic_qsort(a, n, sizeof(int), cmp_int_lt);
}

typedef struct
typedef struct sort_alg
{
sort_func func;
char name[40];
} test_struct;
} sort_alg;




test_struct tests[] =
/*
sort_alg algorithms[] =
{
{ iterative_merge, "itermerge" },
{ heapsort, "hsort" },
@@ -82,9 +87,29 @@ test_struct tests[] =
{ qsort_insertion_wrapper, "qinssort" }
};
*/

sort_alg algorithms[] =
{
{ qsort_wrapper, "qsort" },
{ qsort_lib_wrapper, "qsortlib" },
{ qsort_my_wrapper, "genericqsort" },
{ iter_qsort_wrapper, "iterqsort" },
{ qsort_insertion_wrapper, "qinssort" }
};

#define NUM_ALGS (sizeof(tests)/sizeof(test_struct))
#define NUM_N 13

size_t n2_sizes[] = { 10, 50, 500, 1000, 5000, 10000, 50000 };


size_t n_sizes_small[] = { 10000, 50000, 100000+3, 500000-5, 1000000 };
size_t n_sizes_med[] = { 10000, 50000, 100000+3, 500000-5, 1000000, 5000000, 10000000, 50000000 };
size_t n_sizes_giant[] = { 500000-5, 1000000, 5000000, 10000000, 50000000, 100000000, 500000000 };

size_t n_sizes[] = { 500000, 1000000, 5000000, 10000000, 50000000, 100000000, 500000000 };

#define NUM_ALGS (sizeof(algorithms)/sizeof(sort_alg))
#define NUM_N (sizeof(n_sizes)/sizeof(size_t))



@@ -93,38 +118,40 @@ test_struct tests[] =

int main()
{
int n_sizes[] = {10, 50, 500, 1000, 5000, 10000, 50000, 100000+3, 500000-5, 1000000, 5000000, 10000000, 50000000};
int num_n = (sizeof(n_sizes)/sizeof(int));
//num_n = 6;
int start_n = 5;
int start_n = 0;
int num_n = NUM_N;

double table_results[NUM_ALGS][NUM_N];
clock_t before;

int *test_array, *temp_array;

printf("%u %d\n", (unsigned)sizeof(int), INT_MAX);

size_t i, j, k;

for(int i=start_n; i<num_n; ++i) {
for(i=start_n; i<num_n; ++i) {
srand(time(NULL));
test_array = (int*)malloc(n_sizes[i] * sizeof(int));
temp_array = (int*)malloc(n_sizes[i] * sizeof(int));

for(int j=0; j<n_sizes[i]; ++j) {
temp_array[j] = test_array[j] = rand() % (4*n_sizes[i]);
for(j=0; j<n_sizes[i]; ++j) {
temp_array[j] = test_array[j] = rand();
}

printf("N = %d\n", n_sizes[i]);
printf("N = %lu\n", n_sizes[i]);

//loop through selected algorithms it test_struct
for (int j=0; j<NUM_ALGS; ++j) {
for (j=0; j<NUM_ALGS; ++j) {
before = clock();
tests[j].func(temp_array, n_sizes[i]);
algorithms[j].func(temp_array, n_sizes[i]);
table_results[j][i] = ((double)(clock() - before))/CLOCKS_PER_SEC;

//check results
#ifdef DEBUG
for(int k=1; k<n_sizes[i]; ++k) {
for(k=1; k<n_sizes[i]; ++k) {
if(temp_array[k-1] > temp_array[k]) {
printf("\n\n%s faled\n", tests[j].name);
printf("\n\n%s failed\n", algorithms[j].name);
assert(temp_array[k-1] <= temp_array[k]);
}
}
@@ -142,16 +169,16 @@ int main()
printf("%50s\n", "Sort time in milliseconds");
printf("%10s", "N");
int len;
for (int j=0; j<NUM_ALGS; ++j) {
len = strlen(tests[j].name);
printf("%*s", ((len > 10) ? len+2 : 10), tests[j].name);
for (j=0; j<NUM_ALGS; ++j) {
len = strlen(algorithms[j].name);
printf("%*s", ((len > 10) ? len+2 : 10), algorithms[j].name);
}
putchar('\n');

for (int i=start_n; i<num_n; i++) {
printf("%10d", n_sizes[i]);
for (int j=0; j<NUM_ALGS; j++) {
len = strlen(tests[j].name);
for (i=start_n; i<num_n; i++) {
printf("%10lu", n_sizes[i]);
for (j=0; j<NUM_ALGS; j++) {
len = strlen(algorithms[j].name);
printf("%*.0f", ((len > 10) ? len+2 : 10), table_results[j][i]*1000);
}
putchar('\n');
@@ -161,13 +188,13 @@ int main()
FILE* file = fopen("output.dat", "w");

fprintf(file, "%*sN", 11, " ");
for (int i=start_n; i<num_n; ++i)
fprintf(file, "%12d", n_sizes[i]);
for (i=start_n; i<num_n; ++i)
fprintf(file, "%12lu", n_sizes[i]);
fputc('\n', file);

for (int i=0; i<NUM_ALGS; ++i) {
fprintf(file, "%*s", 12, tests[i].name);
for (int j=start_n; j<num_n; ++j)
for (i=0; i<NUM_ALGS; ++i) {
fprintf(file, "%*s", 12, algorithms[i].name);
for (j=start_n; j<num_n; ++j)
fprintf(file, "%*.0f", 12, table_results[i][j]*1000);
fputc('\n', file);
}

0 comments on commit 8c8c1f2

Please sign in to comment.
You can’t perform that action at this time.