summaryrefslogtreecommitdiff
path: root/src/stdlib
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdlib')
-rw-r--r--src/stdlib/_qsort.h33
-rw-r--r--src/stdlib/qsort.c33
2 files changed, 32 insertions, 34 deletions
diff --git a/src/stdlib/_qsort.h b/src/stdlib/_qsort.h
deleted file mode 100644
index 475932a7..00000000
--- a/src/stdlib/_qsort.h
+++ /dev/null
@@ -1,33 +0,0 @@
-#include <stdlib.h>
-
-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);
-}
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);