toaruos/libc/stdlib/qsort.c

20 lines
509 B
C
Raw Normal View History

2018-04-21 15:34:05 +03:00
#include <stdlib.h>
#include <stddef.h>
2018-04-21 16:08:04 +03:00
#include <string.h>
2018-04-21 15:34:05 +03:00
#include <sys/types.h>
void qsort(void * base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) {
for (size_t i = 0; i < nmemb-1; ++i) {
for (size_t j = 0; j < nmemb-1; ++j) {
void * left = (char *)base + size * j;
void * right = (char *)base + size * (j + 1);
if (compar(left,right) > 0) {
char tmp[size];
memcpy(tmp, right, size);
memcpy(right, left, size);
memcpy(left, tmp, size);
}
}
}
}