add count sort

This commit is contained in:
2018-11-13 09:15:33 -03:00
parent e3f5691462
commit 601b3c95ca
6 changed files with 100 additions and 7 deletions

View File

@@ -2,7 +2,7 @@ CC=gcc
CFLAGS=-Wall -I../src/include -DDEBUG -g
SRC=test.c
OBJ=$(SRC:.c=.o)
OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o
OBJ+=../src/random.o ../src/bubble_sort.o ../src/timer.o ../src/count_sort.o
all: test

View File

@@ -19,6 +19,7 @@
#include <unistd.h>
#include "random.h"
#include "bubble_sort.h"
#include "count_sort.h"
static int *test_case;
static int *test_array;
@@ -50,7 +51,7 @@ int main(int argc, char **argv) {
int n = 50000;
int i;
int gen;
int pass = 1;
int pass;
int passed = 0;
int failed = 0;
@@ -63,6 +64,7 @@ int main(int argc, char **argv) {
fprintf(stdout, "Running tests:\n");
// Test random number generation
pass = 1;
fprintf(stdout, "\trandom generation: ");
fflush(stdout);
for (i = 0; i < 5000000; i++) {
@@ -78,22 +80,22 @@ int main(int argc, char **argv) {
fprintf(stdout, "pass\n");
passed++;
}
pass = 1;
// Prepare for sort tests
for (i = 0; i < n; i++) {
test_case[i] = gen_rand(-100, 100);
}
memcpy(qarray, test_case, sizeof(int) * n);
memcpy(test_array, test_case, sizeof(int) * n);
qsort(qarray, n, sizeof(int), compar);
// Test bubble sort
pass = 1;
memcpy(test_array, test_case, sizeof(int) * n);
fprintf(stdout, "\tbubble sort: ");
fflush(stdout);
bubble_sort(test_array, n);
for (i = 0; i < n; i++) {
if (test_array[i] != qarray[i] && pass) {
if (test_array[i] != qarray[i]) {
fprintf(stdout, "fail\n");
failed++;
pass = 0;
@@ -104,7 +106,25 @@ int main(int argc, char **argv) {
fprintf(stdout, "pass\n");
passed++;
}
// Test count sort
pass = 1;
memcpy(test_array, test_case, sizeof(int) * n);
fprintf(stdout, "\tcount sort: ");
fflush(stdout);
count_sort(test_array, n);
for (i = 0; i < n; i++) {
if (test_array[i] != qarray[i]) {
fprintf(stdout, "fail\n");
failed++;
pass = 0;
break;
}
}
if (pass) {
fprintf(stdout, "pass\n");
passed++;
}
fprintf(stdout, "%d tests passed\n", passed);
fprintf(stdout, "%d tests failed\n", failed);