summaryrefslogtreecommitdiff
path: root/src/stdlib/qsort.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdlib/qsort.c')
-rw-r--r--src/stdlib/qsort.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c
index b8e21b9c..0c7d8e21 100644
--- a/src/stdlib/qsort.c
+++ b/src/stdlib/qsort.c
@@ -1,8 +1,39 @@
#include <stdlib.h>
-#include "_qsort.h"
/** sort an array **/
+static void __swap(char *base, size_t size, size_t b, size_t c)
+{
+ char *x = base + (size * b);
+ char *y = base + (size * c);
+ size_t i;
+
+ for (i = 0; i < size; i++) {
+ char tmp = x[i];
+ x[i] = y[i];
+ y[i] = tmp;
+ }
+}
+
+static void __qsort(char *base, size_t size, size_t lo, size_t hi, int (*compar)(const void *, const void *))
+{
+ size_t i = lo, j = 0;
+ if (lo >= hi) {
+ return;
+ }
+
+ for (j = lo; j < hi; j++) {
+ if (compar(base + (size * j), base + (size * hi)) < 0) {
+ __swap(base, size, i, j);
+ i++;
+ }
+ }
+ __swap(base, size, i, hi);
+
+ __qsort(base, size, lo, i - 1, compar);
+ __qsort(base, size, i + 1, hi, compar);
+}
+
void qsort(void * base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
{
__qsort(base, size, 0, nmemb - 1, compar);