Add ArraySort()

This commit is contained in:
Jordan Bancino 2022-07-27 15:31:34 -04:00
parent f18d47757c
commit 39af3c2194
2 changed files with 53 additions and 0 deletions

View file

@ -174,3 +174,53 @@ ArrayTrim(Array * array)
return 1; return 1;
} }
static void
ArraySwap(Array * array, size_t i, size_t j)
{
void *p = array->entries[i];
array->entries[i] = array->entries[j];
array->entries[j] = p;
}
static size_t
ArrayPartition(Array * array, size_t low, size_t high, int (*compare) (void *, void *))
{
void *pivot = array->entries[high];
size_t i = low - 1;
size_t j;
for (j = low; j <= high - 1; j++)
{
if (compare(array->entries[j], pivot) < 0)
{
i++;
ArraySwap(array, i, j);
}
}
ArraySwap(array, i + 1, high);
return i + 1;
}
static void
ArrayQuickSort(Array * array, size_t low, size_t high, int (*compare) (void *, void *))
{
if (low < high)
{
size_t pi = ArrayPartition(array, low, high, compare);
ArrayQuickSort(array, low, pi - 1, compare);
ArrayQuickSort(array, pi + 1, high, compare);
}
}
void
ArraySort(Array * array, int (*compare) (void *, void *))
{
if (!array)
{
return;
}
ArrayQuickSort(array, 0, array->size, compare);
}

View file

@ -23,6 +23,9 @@ extern int
extern void * extern void *
ArrayDelete(Array * array, size_t index); ArrayDelete(Array * array, size_t index);
extern void
ArraySort(Array *, int (*compare) (void *, void *));
extern void extern void
ArrayFree(Array * array); ArrayFree(Array * array);