From 861088152f3c0f11ed2a14f4aa5ef18c6076f63b Mon Sep 17 00:00:00 2001 From: lucielle Date: Fri, 15 May 2026 23:24:04 -0500 Subject: [PATCH] reimplement compare function for quicksort --- include/quicksort.h | 2 +- include/types.h | 3 +++ quicksort.c | 18 +++++++++--------- test/test.c | 6 +++++- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/include/quicksort.h b/include/quicksort.h index c88d5e0..1b0c80a 100644 --- a/include/quicksort.h +++ b/include/quicksort.h @@ -3,6 +3,6 @@ #include "types.h" -int lsort_quicksort(lsort_array* array); +int lsort_quicksort(lsort_array* array, int_function_pointer compare_function); #endif \ No newline at end of file diff --git a/include/types.h b/include/types.h index 504a38f..2332f84 100644 --- a/include/types.h +++ b/include/types.h @@ -4,6 +4,7 @@ #include #include "string.h" + typedef struct { const char *buf; size_t len; @@ -13,6 +14,8 @@ typedef struct { int key; void* value; } lsort_item; +typedef int (*int_function_pointer)(lsort_item*, lsort_item*); + typedef struct { size_t count; diff --git a/quicksort.c b/quicksort.c index 895e273..3994924 100644 --- a/quicksort.c +++ b/quicksort.c @@ -2,12 +2,12 @@ #include "include/types.h" #include "include/quicksort.h" -static int partition(lsort_array* array, int L, int R) { - int pivot = array->items[R]->key; +static int partition(lsort_array* array, int L, int R, int_function_pointer compare_function) { + lsort_item* pivot = array->items[R]; int i = L - 1; for (int j = L; j < R; j++) { - if (array->items[j]->key < pivot) { + if (compare_function(array->items[j], pivot) < 0) { i = i + 1; lsort_array_swap(array, i, j); } @@ -17,20 +17,20 @@ static int partition(lsort_array* array, int L, int R) { return i + 1; } -static void _quicksort(lsort_array* array, int L, int R) { +static void _quicksort(lsort_array* array, int L, int R, int_function_pointer compare_function) { if (L < R) { - int pivotIndex = partition(array, L, R); - _quicksort(array, L, pivotIndex - 1); - _quicksort(array, pivotIndex + 1, 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* array) { +int lsort_quicksort(lsort_array* array, int_function_pointer compare_function) { if (array->count < 1) { return 0; } - _quicksort(array, 0, (int)array->count - 1); + _quicksort(array, 0, (int)array->count - 1, compare_function); return check_sorted(array); } \ No newline at end of file diff --git a/test/test.c b/test/test.c index 3627dc3..35ae83f 100644 --- a/test/test.c +++ b/test/test.c @@ -75,11 +75,15 @@ int test_radix_sort() { return return_code; } +int test_compare_function(lsort_item* a, lsort_item* b) { + return a->key - b->key; +} + int test_quicksort() { printf("lsort_quicksort()...\n"); lsort_array* array_quicksort = generate_samples(sample_count); - int return_code = lsort_quicksort(array_quicksort); + int return_code = lsort_quicksort(array_quicksort, test_compare_function); lsort_array_destroy(array_quicksort, 1); return return_code;