2000-05-28 13:40:48 +00:00
|
|
|
/*
|
|
|
|
* qsort.c
|
|
|
|
*
|
|
|
|
* Ullrich von Bassewitz, 09.12.1998
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-11 18:16:17 +00:00
|
|
|
static void QuickSort (register unsigned char* Base, int Lo, int Hi,
|
|
|
|
register size_t Size,
|
2000-05-28 13:40:48 +00:00
|
|
|
int (*Compare)(const void*, const void*))
|
|
|
|
/* Internal recursive function. Works with ints, but this shouldn't be
|
|
|
|
* a problem.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
int I, J;
|
|
|
|
|
|
|
|
/* Quicksort */
|
|
|
|
while (Hi > Lo) {
|
|
|
|
I = Lo + Size;
|
|
|
|
J = Hi;
|
|
|
|
while (I <= J) {
|
2012-06-11 18:16:17 +00:00
|
|
|
while (I <= J && Compare (Base + Lo, Base + I) >= 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
I += Size;
|
|
|
|
}
|
2012-06-11 18:16:17 +00:00
|
|
|
while (I <= J && Compare (Base + Lo, Base + J) < 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
J -= Size;
|
|
|
|
}
|
|
|
|
if (I <= J) {
|
2012-06-11 18:16:17 +00:00
|
|
|
_swap (Base + I, Base + J, Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
I += Size;
|
|
|
|
J -= Size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (J != Lo) {
|
2012-06-11 18:16:17 +00:00
|
|
|
_swap (Base + J, Base + Lo, Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
if (((unsigned) J) * 2 > (Hi + Lo)) {
|
|
|
|
QuickSort (Base, J + Size, Hi, Size, Compare);
|
|
|
|
Hi = J - Size;
|
|
|
|
} else {
|
|
|
|
QuickSort (Base, Lo, J - Size, Size, Compare);
|
|
|
|
Lo = J + Size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-10-29 21:59:19 +00:00
|
|
|
void __fastcall__ qsort (void* base, size_t nmemb, size_t size,
|
|
|
|
int (*compare)(const void*, const void*))
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Quicksort implementation */
|
|
|
|
{
|
|
|
|
if (nmemb > 1) {
|
|
|
|
QuickSort (base, 0, (nmemb-1) * size, size, compare);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|