qsort.c 541 B

123456789101112131415161718192021
  1. /* qsort: sort v[left]...v[right] into increasing order */
  2. void
  3. qsort (void *v[], int left, int right,
  4. int (*comp)(void *, void *))
  5. {
  6. int i, last;
  7. void swap(void *v[], int, int);
  8. /* do nothing if array contains
  9. ** fewer than two elements */
  10. if (left >= right)
  11. return;
  12. swap (v, left, (left + right) / 2);
  13. last = left;
  14. for (i = left + 1; i <= right; i++)
  15. if ((*comp)(v[i], v[left]) < 0)
  16. swap (v, ++last, i);
  17. swap (v, left, last);
  18. qsort (v, left, last - 1, comp);
  19. qsort (v, last + 1, right, comp);
  20. }