Improve qsort_r(3) run-time complexity using quicksort.
This commit is contained in:
parent
a9cf3cc684
commit
f3eb87c3ee
|
@ -22,13 +22,12 @@
|
||||||
|
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static void memswap(uint8_t* a, uint8_t* b, size_t size)
|
static void memswap(unsigned char* a, unsigned char* b, size_t size)
|
||||||
{
|
{
|
||||||
uint8_t tmp;
|
unsigned char tmp;
|
||||||
for ( size_t i = 0; i < size; i++ )
|
for ( size_t i = 0; i < size; i++ )
|
||||||
{
|
{
|
||||||
tmp = a[i];
|
tmp = a[i];
|
||||||
|
@ -37,8 +36,51 @@ static void memswap(uint8_t* a, uint8_t* b, size_t size)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: This is just a quick and dirty insertion sort. It'd be nice to have a
|
static
|
||||||
// good old quick sort here soon.
|
unsigned char* array_index(unsigned char* base,
|
||||||
|
size_t element_size,
|
||||||
|
size_t index)
|
||||||
|
{
|
||||||
|
return base + element_size * index;
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
size_t partition(unsigned char* base,
|
||||||
|
size_t element_size,
|
||||||
|
size_t num_elements,
|
||||||
|
size_t pivot_index,
|
||||||
|
int (*compare)(const void*, const void*, void*),
|
||||||
|
void* arg)
|
||||||
|
{
|
||||||
|
if ( pivot_index != num_elements - 1 )
|
||||||
|
{
|
||||||
|
unsigned char* pivot = array_index(base, element_size, pivot_index);
|
||||||
|
unsigned char* other = array_index(base, element_size, num_elements - 1);
|
||||||
|
memswap(pivot, other, element_size);
|
||||||
|
pivot_index = num_elements - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t store_index = 0;
|
||||||
|
for ( size_t i = 0; i < num_elements - 1; i++ )
|
||||||
|
{
|
||||||
|
unsigned char* pivot = array_index(base, element_size, pivot_index);
|
||||||
|
unsigned char* value = array_index(base, element_size, i);
|
||||||
|
if ( compare(value, pivot, arg) <= 0 )
|
||||||
|
{
|
||||||
|
unsigned char* other = array_index(base, element_size, store_index);
|
||||||
|
if ( value != other )
|
||||||
|
memswap(value, other, element_size);
|
||||||
|
store_index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char* pivot = array_index(base, element_size, pivot_index);
|
||||||
|
unsigned char* value = array_index(base, element_size, store_index);
|
||||||
|
memswap(pivot, value, element_size);
|
||||||
|
|
||||||
|
return store_index;
|
||||||
|
}
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
void qsort_r(void* base_ptr,
|
void qsort_r(void* base_ptr,
|
||||||
size_t num_elements,
|
size_t num_elements,
|
||||||
|
@ -46,19 +88,18 @@ void qsort_r(void* base_ptr,
|
||||||
int (*compare)(const void*, const void*, void*),
|
int (*compare)(const void*, const void*, void*),
|
||||||
void* arg)
|
void* arg)
|
||||||
{
|
{
|
||||||
uint8_t* base = (uint8_t*) base_ptr;
|
unsigned char* base = (unsigned char*) base_ptr;
|
||||||
for ( size_t i = 0; i < num_elements; i++ )
|
|
||||||
{
|
if ( !element_size || num_elements < 2 )
|
||||||
for ( size_t c = i; c; c-- )
|
return;
|
||||||
{
|
|
||||||
size_t currentoff = c * element_size;
|
size_t pivot_index = num_elements / 2;
|
||||||
uint8_t* current = base + currentoff;
|
pivot_index = partition(base, element_size, num_elements, pivot_index, compare, arg);
|
||||||
size_t p = c-1;
|
|
||||||
size_t prevoff = p * element_size;
|
if ( 2 <= pivot_index )
|
||||||
uint8_t* prev = base + prevoff;
|
qsort_r(base, pivot_index, element_size, compare, arg);
|
||||||
int cmp = compare(prev, current, arg);
|
|
||||||
if ( cmp <= 0 ) { break; }
|
if ( 2 <= num_elements - (pivot_index + 1) )
|
||||||
memswap(prev, current, element_size);
|
qsort_r(array_index(base, element_size, pivot_index + 1),
|
||||||
}
|
num_elements - (pivot_index + 1), element_size, compare, arg);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue