Advertisement
aaaaaa123456789

JV's programming challenge -- week 2, version 2

Nov 24th, 2012
69
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.   if (arrayLength < 2) return;
  5.   int * first = array;
  6.   int * last = array + (arrayLength - 1);
  7.   int * current;
  8.   int temp = 1;
  9.   for (current = first; temp && (current < last); current ++)
  10.     if (*current < current[1])
  11.       temp = 0;
  12.   if (temp) return;
  13.   if (arrayLength == 2) {
  14.     temp = *first;
  15.     *first = *last;
  16.     *last = temp;
  17.     return;
  18.   }
  19.   current = last;
  20.   while (first != last) {
  21.     if (*first < *last) {
  22.       temp = *first;
  23.       *first = *last;
  24.       *last = temp;
  25.       current = (current == first) ? last : first;
  26.     }
  27.     if (current == last)
  28.       first ++;
  29.     else
  30.       last --;
  31.   }
  32.   sort(array, current - array);
  33.   sort(current + 1, arrayLength - 1 - (current - array));
  34. }
Advertisement
RAW Paste Data Copied
Advertisement