#include "distribution_counting.h"
/* [Distribution Counting Sort]
* First used with radix sorting by H.Seward, 1954.
* Then published with "Mathsort" by W.Feurzeig, CACM 3 (1960), 601
*/
static int *counting_table = NULL;
static int cur_table_len = 0;
static int
create_counting_table(int cnt)
{
F_S();
counting_table = (int*)malloc(sizeof(int) * cnt);
if(counting_table == NULL) {
debug("malloc failed. [%s] \r\n", __FUNCTION__);
return 0;
}
cur_table_len = cnt;
F_E();
return 1;
}
void
free_counting_table(void)
{
F_S();
if (counting_table == NULL) return;
free(counting_table);
counting_table = NULL;
cur_table_len = 0;
F_E();
}
/* we guess all the items located in [@min, @max],
* and do the counting for sorting.
*/
int*
do_distribution_counting_sort(void* src, int src_sz, int item_cnt, int min, int max, void *out,
GET_ITEM_FUNC get_item, SET_ITEM_FUNC set_item)
{
int rt;
int i,j;
char *li,*ri;
long ki;
F_S();
if (src == NULL || src_sz <= 0 || item_cnt <= 0) return NULL;
if (min < 0 || max <= 0 || min > max) return NULL;
/* try create auxiliary table for counting
*/
if (counting_table &#
Foundation: Distribution Counting Sort
最新推荐文章于 2018-04-04 11:43:00 发布