qsort.c 618 B

12345678910111213141516171819202122232425
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. extern char * _argv_0;
  7. extern int __libc_debug;
  8. void qsort(void * base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) {
  9. if (!nmemb) return;
  10. if (!size) return;
  11. for (size_t i = 0; i < nmemb-1; ++i) {
  12. for (size_t j = 0; j < nmemb-1; ++j) {
  13. void * left = (char *)base + size * j;
  14. void * right = (char *)base + size * (j + 1);
  15. if (compar(left,right) > 0) {
  16. char tmp[size];
  17. memcpy(tmp, right, size);
  18. memcpy(right, left, size);
  19. memcpy(left, tmp, size);
  20. }
  21. }
  22. }
  23. }