2000-05-28 13:40:48 +00:00
|
|
|
/*
|
2014-06-30 09:10:35 +00:00
|
|
|
** qsort.c
|
|
|
|
**
|
2015-06-22 04:15:48 +00:00
|
|
|
** 1998.12.09, Ullrich von Bassewitz
|
|
|
|
** 2015-06-21, Greg King
|
2014-06-30 09:10:35 +00:00
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#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,
|
2015-06-22 04:15:48 +00:00
|
|
|
int __fastcall__ (* Compare) (const void*, const void*))
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Internal recursive function. Works with ints, but this shouldn't be
|
2014-06-30 09:10:35 +00:00
|
|
|
** a problem.
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
|
|
|
int I, J;
|
|
|
|
|
|
|
|
/* Quicksort */
|
|
|
|
while (Hi > Lo) {
|
2013-05-09 11:56:54 +00:00
|
|
|
I = Lo + Size;
|
|
|
|
J = Hi;
|
|
|
|
while (I <= J) {
|
|
|
|
while (I <= J && Compare (Base + Lo, Base + I) >= 0) {
|
|
|
|
I += Size;
|
|
|
|
}
|
|
|
|
while (I <= J && Compare (Base + Lo, Base + J) < 0) {
|
|
|
|
J -= Size;
|
|
|
|
}
|
|
|
|
if (I <= J) {
|
2022-08-29 18:52:35 +00:00
|
|
|
__swap (Base + I, Base + J, Size);
|
2013-05-09 11:56:54 +00:00
|
|
|
I += Size;
|
|
|
|
J -= Size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (J != Lo) {
|
2022-08-29 18:52:35 +00:00
|
|
|
__swap (Base + J, Base + Lo, Size);
|
2013-05-09 11:56:54 +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;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-10-29 21:59:19 +00:00
|
|
|
void __fastcall__ qsort (void* base, size_t nmemb, size_t size,
|
2015-06-22 04:15:48 +00:00
|
|
|
int __fastcall__ (* compare) (const void*, const void*))
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Quicksort implementation */
|
|
|
|
{
|
|
|
|
if (nmemb > 1) {
|
2013-05-09 11:56:54 +00:00
|
|
|
QuickSort (base, 0, (nmemb-1) * size, size, compare);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|