1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-06 16:59:03 +03:00
systemd/klibc/klibc/qsort.c
greg@kroah.com a41a0e28c2 [PATCH] added klibc version 0.82 (cvs tree) to the udev tree.
Not hooked up to the build yet.
2005-04-26 21:05:23 -07:00

43 lines
789 B
C

/*
* qsort.c
*
* This is actually combsort. It's an O(n log n) algorithm with
* simplicity/small code size being its main virtue.
*/
#include <stddef.h>
#include <string.h>
static inline size_t newgap(size_t gap)
{
gap = (gap*10)/13;
if ( gap == 9 || gap == 10 )
gap = 11;
if ( gap < 1 )
gap = 1;
return gap;
}
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
{
size_t gap = nmemb;
size_t i, j;
void *p1, *p2;
int swapped;
do {
gap = newgap(gap);
swapped = 0;
for ( i = 0, p1 = base ; i < nmemb-gap ; i++, (char *)p1 += size ) {
j = i+gap;
if ( compar(p1, p2 = (char *)base+j*size) > 0 ) {
memswap(p1, p2, size);
swapped = 1;
}
}
} while ( gap > 1 || swapped );
}