From 4830af498e2d3a5440e65c63a3595b91f1cd4ac9 Mon Sep 17 00:00:00 2001 From: Pixel Date: Sat, 5 Feb 2011 09:40:03 -0800 Subject: Added qsort, stolen from libnix. --- libc/src/qsort.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 libc/src/qsort.c (limited to 'libc/src/qsort.c') diff --git a/libc/src/qsort.c b/libc/src/qsort.c new file mode 100755 index 0000000..eeb038b --- /dev/null +++ b/libc/src/qsort.c @@ -0,0 +1,51 @@ +#include +#include + +/* sample compar function: int cmp(void *a,void *b){ return *(int *)a-*(int *)b; } */ + +/* This qsort function does a little trick: + * To reduce stackspace it iterates the larger interval instead of doing + * the recursion on both intervals. + * So stackspace is limited to 32*stack_for_1_iteration = + * 32*4*(4 arguments+1 returnaddress+11 stored registers) = 2048 Bytes, + * which is small enough for everybodys use. + * (And this is the worst case if you own 4GB and sort an array of chars.) + * Sparing the function calling overhead does improve performance, too. + */ + +void qsort +(void *base,size_t nmemb,size_t size,int (*compar)(const void *,const void *)) +{ char *base2=(char *)base; + size_t i,a,b,c; + while(nmemb>1) + { a=0; + b=nmemb-1; + c=(a+b)/2; /* Middle element */ + for(;;) + { while((*compar)(&base2[size*c],&base2[size*a])>0) + a++; /* Look for one >= middle */ + while((*compar)(&base2[size*c],&base2[size*b])<0) + b--; /* Look for one <= middle */ + if(a>=b) + break; /* We found no pair */ + for(i=0;i