aaaaaa123456789 icon

JV's programming challenge week 2

aaaaaa123456789 | PRO | 11/19/12 02:41:58 AM UTC | 0 ⭐ | 436 👁️ | Never ⏰ | []
C |

960 B

|

None

|

0 👍

/

0 👎

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

Comments