From d06225647ebf64f2445391ee7c73e19ba216f3eb Mon Sep 17 00:00:00 2001 From: Kray-G Date: Mon, 11 Nov 2019 12:46:50 +0900 Subject: [PATCH 1/2] issue #17: added qsort() in stdlib. --- kccrt/libsrc/stdlib.c | 38 ++++++++++++++++++++++++++++++++++++++ samples/qsort.c | 18 ++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 samples/qsort.c diff --git a/kccrt/libsrc/stdlib.c b/kccrt/libsrc/stdlib.c index c8d23f8..f1efc84 100644 --- a/kccrt/libsrc/stdlib.c +++ b/kccrt/libsrc/stdlib.c @@ -18,4 +18,42 @@ int rand(void) return (unsigned)(__kcc_rand_seed / 65536L) % (RAND_MAX+1); } +#define SWAP(x, y, size) \ + do { \ + size_t s = (size); \ + char *a = (x), *b = (y); \ + do { \ + char t = *a; \ + *a++ = *b; \ + *b++ = t; \ + } while (--s > 0); \ + } while (0) \ + /**/ + +#define KCCQ(a, i) ((char *)a + (i) * size) + +void __quicksort(int first, int last, void *base, size_t size, int (*comp)(const void *, const void *)) +{ + int i, j; + void *x = KCCQ(base, (first + last) / 2); + i = first; j = last; + for ( ; ; ) { + while (comp(x, KCCQ(base, i)) > 0) i++; + while (comp(KCCQ(base, j), x) > 0) j--; + if (i >= j) break; + SWAP(KCCQ(base,i), KCCQ(base,j), size); + i++; + j--; + } + if (first < i - 1) __quicksort(first , i - 1, base, size, comp); + if (j + 1 < last) __quicksort(j + 1, last, base, size, comp); +} + +#undef KCCQ + +void qsort(void *base, size_t n, size_t size, int (*comp)(const void *, const void *)) +{ + __quicksort(0, n - 1, base, size, comp); +} + #endif diff --git a/samples/qsort.c b/samples/qsort.c new file mode 100644 index 0000000..98e210c --- /dev/null +++ b/samples/qsort.c @@ -0,0 +1,18 @@ +#include +#include + +int cmp(const void *p, const void *q) +{ + return *(int*)p - *(int*)q; +} + +main() +{ + int values[] = { 49, 2, 180, 23, 77, 13, 29, 88 }; + int n, i; + + n = sizeof(values)/sizeof(int); + qsort(values, n, sizeof(int), cmp); + for (i = 0; i < n; i++) + printf("values[%d] = %d\n", i, values[i]); +} From bf0fe554e8bb0ed7b16af6b844ba8e25a291c4c3 Mon Sep 17 00:00:00 2001 From: Kray-G Date: Mon, 11 Nov 2019 14:29:39 +0900 Subject: [PATCH 2/2] issue #17: updated sample for qsort. --- samples/qsort.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/samples/qsort.c b/samples/qsort.c index 98e210c..cabf020 100644 --- a/samples/qsort.c +++ b/samples/qsort.c @@ -6,13 +6,22 @@ int cmp(const void *p, const void *q) return *(int*)p - *(int*)q; } -main() +#define N 20 +int a[N]; + +int main(void) { - int values[] = { 49, 2, 180, 23, 77, 13, 29, 88 }; - int n, i; + int i; - n = sizeof(values)/sizeof(int); - qsort(values, n, sizeof(int), cmp); - for (i = 0; i < n; i++) - printf("values[%d] = %d\n", i, values[i]); + printf("Before:"); + for (i = 0; i < N; i++) { + a[i] = rand() / (RAND_MAX / 100 + 1); + printf(" %2d", a[i]); + } + printf("\n"); + qsort(a, sizeof(a)/sizeof(a[0]), sizeof(a[0]), cmp); + printf("After: "); + for (i = 0; i < N; i++) printf(" %2d", a[i]); + printf("\n"); + return 0; }