36 lines
834 B
C
36 lines
834 B
C
#include "include/lsort.h"
|
|
#include "include/types.h"
|
|
#include "include/quicksort.h"
|
|
|
|
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 (array->items[j]->key < pivot) {
|
|
i = i + 1;
|
|
lsort_array_swap(array, i, j);
|
|
}
|
|
}
|
|
|
|
lsort_array_swap(array, i + 1, R);
|
|
return i + 1;
|
|
}
|
|
|
|
static void _quicksort(lsort_array* array, int L, int R) {
|
|
if (L < R) {
|
|
int pivotIndex = partition(array, L, R);
|
|
_quicksort(array, L, pivotIndex - 1);
|
|
_quicksort(array, pivotIndex + 1, R);
|
|
}
|
|
}
|
|
|
|
int lsort_quicksort(lsort_array* array) {
|
|
if (array->count < 1) {
|
|
return 0;
|
|
}
|
|
|
|
_quicksort(array, 0, (int)array->count - 1);
|
|
|
|
return check_sorted(array);
|
|
} |