Better memory management of arrays and test sample generation

This commit is contained in:
2026-05-15 13:18:01 -05:00
parent de5493e7c6
commit f186ebfca8
12 changed files with 228 additions and 177 deletions

View File

@@ -1,45 +1,36 @@
#include "include/lsort.h"
#include "include/types.h"
#include "include/quicksort.h"
#include <stdio.h>
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];
static int partition(lsort_array* array, int L, int R) {
int pivot = array->items[R]->key;
int i = L - 1;
for (int j = L; j < R; j++) {
if (compare_function(array->items[j], pivot) < 0) {
if (array->items[j]->key < pivot) {
i = i + 1;
lsort_array_i_swap(array, i, j);
lsort_array_swap(array, i, j);
}
}
lsort_array_i_swap(array, i + 1, R);
lsort_array_swap(array, i + 1, R);
return i + 1;
}
static void _quicksort(lsort_array_i* array, int L, int R, int_function_pointer compare_function) {
static void _quicksort(lsort_array* array, int L, int R) {
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 pivotIndex = partition(array, L, R);
_quicksort(array, L, pivotIndex - 1);
_quicksort(array, pivotIndex + 1, R);
}
}
int lsort_quicksort(lsort_array_i* array, int_function_pointer compare_function) {
int lsort_quicksort(lsort_array* array) {
if (array->count < 1) {
return 0;
}
if (compare_function == NULL) compare_function = default_compare_function;
_quicksort(array, 0, (int)array->count - 1);
_quicksort(array, 0, (int)array->count - 1, compare_function);
return check_sorted(array) ? 1 : 0;
return check_sorted(array);
}