X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;ds=sidebyside;f=src%2Fstdlib%2Fqsort.c;h=da58fd3177814634d7e1b9b06ea2135ee633295f;hb=de7dc1318f493184b20f7661bc12b1829b957b67;hp=866af0ecd6c2a394f896a8e3d8a0403e9da94577;hpb=22263709eda9f7d692a0f484fd759f757418dbd7;p=musl diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c index 866af0ec..da58fd31 100644 --- a/src/stdlib/qsort.c +++ b/src/stdlib/qsort.c @@ -21,6 +21,9 @@ /* Minor changes by Rich Felker for integration in musl, 2011-04-27. */ +/* Smoothsort, an adaptive variant of Heapsort. Memory usage: O(1). + Run time: Worst case O(n log n), close to O(n) in the mostly-sorted case. */ + #include #include #include @@ -155,12 +158,16 @@ void qsort(void *base, size_t nel, size_t width, cmpfun cmp) { size_t lp[12*sizeof(size_t)]; size_t i, size = width * nel; - unsigned char *head = base, - *high = head + size - width; + unsigned char *head, *high; size_t p[2] = {1, 0}; int pshift = 1; int trail; + if (!size) return; + + head = base; + high = head + size - width; + /* Precompute Leonardo numbers, scaled by element width */ for(lp[0]=lp[1]=width, i=2; (lp[i]=lp[i-2]+lp[i-1]+width) < size; i++);