Basic integer sorting function
#include <stdlib.h>
#include <string.h>
void sort (int * array, unsigned length) {
if (!array) return;
if (length < 2) return;
int * sorted = malloc(length * sizeof(int));
int * lower = sorted;
int * higher = sorted + length;
unsigned p;
for (p = 1; p < length; p ++)
if (array[p] < *array)
*(lower ++) = array[p];
else
*(-- higher) = array[p];
*lower = *array;
p = lower - sorted;
memcpy(array, sorted, sizeof(int) * length);
free(sorted);
sort(array, p ++);
sort(array + p, length - p);
}
Comments