Files
lsort/quicksort.c

36 lines
1.0 KiB
C
Raw Normal View History

2026-05-08 04:33:59 -05:00
#include "include/lsort.h"
#include "include/types.h"
#include "include/quicksort.h"
static int partition(lsort_array* array, int L, int R, int_function_pointer compare_function) {
lsort_item* pivot = array->items[R];
2026-05-08 04:33:59 -05:00
int i = L - 1;
for (int j = L; j < R; j++) {
if (compare_function(array->items[j], pivot) < 0) {
2026-05-08 04:33:59 -05:00
i = i + 1;
lsort_array_swap(array, i, j);
2026-05-08 04:33:59 -05:00
}
}
lsort_array_swap(array, i + 1, R);
2026-05-08 04:33:59 -05:00
return i + 1;
}
static void _quicksort(lsort_array* array, int L, int R, int_function_pointer compare_function) {
2026-05-08 04:33:59 -05:00
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);
2026-05-08 04:33:59 -05:00
}
}
int lsort_quicksort(lsort_array* array, int_function_pointer compare_function) {
2026-05-08 04:33:59 -05:00
if (array->count < 1) {
return 0;
}
_quicksort(array, 0, (int)array->count - 1, compare_function);
2026-05-08 04:33:59 -05:00
return check_sorted(array);
2026-05-08 04:33:59 -05:00
}