aaaaaa123456789 icon

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

aaaaaa123456789 | PRO | 11/24/12 11:22:17 PM UTC | 0 ⭐ | 362 👁️ | Never ⏰ | []
C |

800 B

|

None

|

0 👍

/

0 👎

void sort(int *, unsigned);
 
void sort (int * array, unsigned arrayLength) {
  if (arrayLength < 2) return;
  int * first = array;
  int * last = array + (arrayLength - 1);
  int * current;
  int temp = 1;
  for (current = first; temp && (current < last); current ++)
    if (*current < current[1])
      temp = 0;
  if (temp) return;
  if (arrayLength == 2) {
    temp = *first;
    *first = *last;
    *last = temp;
    return;
  }
  current = last;
  while (first != last) {
    if (*first < *last) {
      temp = *first;
      *first = *last;
      *last = temp;
      current = (current == first) ? last : first;
    }
    if (current == last)
      first ++;
    else
      last --;
  }
  sort(array, current - array);
  sort(current + 1, arrayLength - 1 - (current - array));
}

Comments