Advertisement
aaaaaa123456789

JV's programming challenge week 2

Nov 18th, 2012
107
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void sort (int *, unsigned);
  2.  
  3. void sort (int * array, unsigned arrayLength) {
  4.   int * first = array;
  5.   int * last = array + (arrayLength - 1);
  6.   int * current = first;
  7.   int temp;
  8.   while (first != last) {
  9.     if (*first < *last) {
  10.       temp = *first;
  11.       *first = *last;
  12.       *last = temp;
  13.       current = (current == first) ? last : first;
  14.     }
  15.     if (current == last)
  16.       first ++;
  17.     else
  18.       last --;
  19.   }
  20.   if ((current - array) > 1)
  21.     if ((current - array) == 2) {
  22.       if (*array < array[1]) {
  23.         temp = *array;
  24.         *array = array[1];
  25.         array[1] = *array;
  26.       }
  27.     } else
  28.       sort(array, current - array);
  29.   if ((current - array) < (arrayLength - 2))
  30.     if ((current - array) == (arrayLength - 3)) {
  31.       if (current[1] < current[2]) {
  32.         temp = current[1];
  33.         current[1] = current[2];
  34.         current[2] = temp;
  35.       }
  36.     } else
  37.       sort(current + 1, arrayLength - 1 - (current - array));
  38. }
Advertisement
RAW Paste Data Copied
Advertisement