diff --git a/Makefile b/Makefile index 3a82799..c3c65bd 100644 --- a/Makefile +++ b/Makefile @@ -19,11 +19,13 @@ LIB_INSTALL_PATH := /usr/local/lib STATIC_BUILD_FILES := $(STATIC_BUILD_DIR)/lsort.o \ $(STATIC_BUILD_DIR)/types.o \ - $(STATIC_BUILD_DIR)/radix.o + $(STATIC_BUILD_DIR)/radix.o \ + $(STATIC_BUILD_DIR)/quicksort.o DYNAMIC_BUILD_FILES := $(DYNAMIC_BUILD_DIR)/lsort.o \ $(STATIC_BUILD_DIR)/types.o \ - $(STATIC_BUILD_DIR)/radix.o + $(STATIC_BUILD_DIR)/radix.o \ + $(STATIC_BUILD_DIR)/quicksort.o TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \ @@ -47,21 +49,24 @@ static: $(STATIC_BUILD_FILES) $(STATIC_BUILD_DIR)/%.o: %.c | $(STATIC_BUILD_DIR) $(CC) $(CFLAGS) -c $< -o $@ -test: dynamic static $(TEST_BUILD_FILES) - $(CC) $(CFLAGS) -L$(CURDIR)/$(DYNAMIC_BUILD_DIR) -llsort -o $(TEST_BUILD_DIR)/test_dynamic $(TEST_BUILD_FILES) - $(CC) $(CFLAGS) -o $(TEST_BUILD_DIR)/test_static $(TEST_BUILD_FILES) $(STATIC_BUILD_DIR)/liblsort.a +test: $(TEST_BUILD_FILES) test_dynamic test_static + +test_dynamic: dynamic $(TEST_BUILD_FILES) + $(CC) $(CFLAGS) -L$(CURDIR)/$(DYNAMIC_BUILD_DIR) -llsort -o $(TEST_BUILD_DIR)/test_dynamic $(TEST_BUILD_FILES) - @echo "" @echo "Running dynamic test..." @echo "" LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/build/dynamic ./$(TEST_BUILD_DIR)/test_dynamic + +test_static: static $(TEST_BUILD_FILES) + $(CC) $(CFLAGS) -o $(TEST_BUILD_DIR)/test_static $(TEST_BUILD_FILES) $(STATIC_BUILD_DIR)/liblsort.a @echo "" @echo "Running static test..." @echo "" - + ./$(TEST_BUILD_DIR)/test_static $(TEST_BUILD_DIR)/%.o: test/%.c | $(TEST_BUILD_DIR) diff --git a/include/quicksort.h b/include/quicksort.h new file mode 100644 index 0000000..19f6f2a --- /dev/null +++ b/include/quicksort.h @@ -0,0 +1,8 @@ +#ifndef LSORT_QUICKSORT_H +#define LSORT_QUICKSORT_H + +#include "types.h" + +int lsort_quicksort(lsort_array_i* array, int (*function_pointer)(int, int)); + +#endif \ No newline at end of file diff --git a/quicksort.c b/quicksort.c new file mode 100644 index 0000000..f2f93e2 --- /dev/null +++ b/quicksort.c @@ -0,0 +1,45 @@ +#include "include/lsort.h" +#include "include/types.h" +#include "include/quicksort.h" +#include + +typedef int (*int_function_pointer)(int, int); + +static int default_compare_function(int a, int b) { + return a - b; +} + +static int partition(lsort_array_i* array, int L, int R, int_function_pointer compare_function) { + int pivot = array->items[R]; + int i = L - 1; + + for (int j = L; j < R; j++) { + if (compare_function(array->items[j], pivot) < 0) { + i = i + 1; + lsort_array_i_swap(array, i, j); + } + } + + lsort_array_i_swap(array, i + 1, R); + return i + 1; +} + +static void _quicksort(lsort_array_i* array, int L, int R, int_function_pointer compare_function) { + if (L < R) { + int pivotIndex = partition(array, L, R, compare_function); + _quicksort(array, L, pivotIndex - 1, compare_function); + _quicksort(array, pivotIndex + 1, R, compare_function); + } +} + +int lsort_quicksort(lsort_array_i* array, int_function_pointer compare_function) { + if (array->count < 1) { + return 0; + } + + if (compare_function == NULL) compare_function = default_compare_function; + + _quicksort(array, 0, (int)array->count - 1, compare_function); + + return check_sorted(array) ? 1 : 0; +} \ No newline at end of file diff --git a/radix.c b/radix.c index 7784991..087eaa6 100644 --- a/radix.c +++ b/radix.c @@ -1,7 +1,7 @@ #include "include/types.h" #include "include/radix.h" -int _seperate_pos_neg(lsort_array_i* array, lsort_array_i* pos_array, lsort_array_i* neg_array) { +static int _seperate_pos_neg(lsort_array_i* array, lsort_array_i* pos_array, lsort_array_i* neg_array) { for (int i = 0; i < array->count; i++) { if ((0x80000000 & array->items[i]) == 0x80000000) { lsort_array_i_append(neg_array, array->items[i]); @@ -12,7 +12,7 @@ int _seperate_pos_neg(lsort_array_i* array, lsort_array_i* pos_array, lsort_arra return 0; } -int _sort_items(lsort_array_i* numbers, lsort_array_i_array* buckets) { +static int _sort_items(lsort_array_i* numbers, lsort_array_i_array* buckets) { int byte_check = 0x0000000F; for (int i = 0; i < 8; i++) { diff --git a/test/test.c b/test/test.c index 2029fe6..daaa227 100644 --- a/test/test.c +++ b/test/test.c @@ -1,10 +1,12 @@ #include "../include/types.h" #include "../include/radix.h" +#include "../include/quicksort.h" #include "../include/lsort.h" #include int test_array_i_append(lsort_array_i* array) { - + printf("lsort_array_i_append()...\n"); + lsort_array_i_append(array, 25); lsort_array_i_append(array, 64); lsort_array_i_append(array, 34); @@ -18,6 +20,8 @@ int test_array_i_append(lsort_array_i* array) { } int test_array_i_swap(lsort_array_i* array) { + printf("lsort_array_i_swap()...\n"); + lsort_array_i_swap(array, 0, 5); lsort_array_i_swap(array, 4, 2); lsort_array_i_swap(array, 2, 3); @@ -28,6 +32,8 @@ int test_array_i_swap(lsort_array_i* array) { } int test_append_swap() { + printf("test_append_swap()...\n"); + lsort_array_i array_append_swap = {0}; if (test_array_i_append(&array_append_swap)) return 1; @@ -50,6 +56,8 @@ int test_append_swap() { } int test_radix_sort() { + printf("lsort_radix()...\n"); + lsort_array_i array_radix = {0}; lsort_array_i_append(&array_radix, -2); lsort_array_i_append(&array_radix, -15); @@ -67,14 +75,36 @@ int test_radix_sort() { return 0; } +int test_quicksort() { + printf("lsort_quicksort()...\n"); + lsort_array_i array_quicksort = {0}; + + lsort_array_i_append(&array_quicksort,6); + lsort_array_i_append(&array_quicksort, 5); + lsort_array_i_append(&array_quicksort, 4); + lsort_array_i_append(&array_quicksort, 3); + lsort_array_i_append(&array_quicksort, 2); + lsort_array_i_append(&array_quicksort, 5); + lsort_array_i_append(&array_quicksort, 1); + lsort_array_i_append(&array_quicksort, 0); + + lsort_quicksort(&array_quicksort, NULL); + + if (check_sorted(&array_quicksort)) return 1; + + + return 0; +} + int main(void) { if ( test_append_swap() || - test_radix_sort() + test_radix_sort() || + test_quicksort() ) return 1; - - printf("Test completed successfully.\n"); + + printf("\nTest completed successfully.\n"); return 0; }