Advertisement
aaaaaa123456789

Basic integer sorting function

Mar 12th, 2015
240
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. void sort (int * array, unsigned length) {
  5.   if (!array) return;
  6.   if (length < 2) return;
  7.   int * sorted = malloc(length * sizeof(int));
  8.   int * lower = sorted;
  9.   int * higher = sorted + length;
  10.   unsigned p;
  11.   for (p = 1; p < length; p ++)
  12.     if (array[p] < *array)
  13.       *(lower ++) = array[p];
  14.     else
  15.       *(-- higher) = array[p];
  16.   *lower = *array;
  17.   p = lower - sorted;
  18.   memcpy(array, sorted, sizeof(int) * length);
  19.   free(sorted);
  20.   sort(array, p ++);
  21.   sort(array + p, length - p);
  22. }
Advertisement
RAW Paste Data Copied
Advertisement