src/plugin/radixSort.c
int RadixSortIndex (
    size_t *x,
    size_t *index,
    const size_t N,
    const size_t dshift,
    const size_t raw,
    const int verbose)
{
    int rc ;
    int i;
    size_t nloops  = 0;
    size_t exp   = 1;
    size_t max   = mf_max_unsigned(x, N);
    size_t min   = mf_min_unsigned(x, N);
    size_t range = max - min + 1;
    size_t ctol  = pow(2, 24);

    size_t shift; size_t loops;
    if (raw) {
        shift = dshift;
        loops = log(max) / log(dshift);
    }
    else {
        loops = 64 / dshift - 1;
        shift = pow(2, dshift);
    }

    for (i = 0; i < N; i++)
        index[i] = i;

    if ( range < ctol ) {
        if ( (rc = CountingSortIndex (x, index, N, min, max)) ) return(rc);
        if ( verbose ) {
            sf_printf("\t\tStep (2.1) counting sort: min = %'lu, max = %'lu\n", min, max);
        }
    }
    else {
        do {
            if ( (rc = RadixSortIndexPass (x, index, N, exp, shift)) ) return(rc);
        } while ( (max > (exp *= shift)) & (nloops++ < loops) );
        if ( verbose ) {
            sf_printf("\t\tStep (2.1) radix sort: loops = %lu, bits = %lu, shift = %'lu\n",
                      nloops, dshift, shift);
        }
    }

    return(0);
}
/**
 * @brief Radix sort on unsigned 64-bit integers with index
 *
 * Perform a radix sort on an array of 64-bit integers. The radix
 * sort performs 64 / d passes of the counting sort, where the set of
 * integers is sorted d-bits at a time. In order to achiave this, we
 * sort x[i] mod 2^(d * (j - 1)) for j = 1 to 64 / d. Smaller values of
 * d will result in a slower sort (more pases of the counting sort are
 * required) but will use less memory.
 *
 * @param x vector of unsigned 64-bit integers to sort
 * @param ix vector of same length where to store the sort index
 * @param N length of array
 * @param dshift number of bits to sort at a time
 * @param raw alternatively, have d be the base power
 * @return stable sorted @x and @ix of the sort
 */
@brief Radix sort on unsigned 64-bit integers with index
Perform a radix sort on an array of 64-bit integers. The radix
sort performs 64 / d passes of the counting sort, where the set of
integers is sorted d-bits at a time. In order to achiave this, we
sort x[i] mod 2^(d * (j - 1)) for j = 1 to 64 / d. Smaller values of
d will result in a slower sort (more pases of the counting sort are
required) but will use less memory.


@brief Radix sort on unsigned 64 - bit integers with index Perform a radix sort on an array of 64 - bit integers . The radix sort performs 64 / d passes of the counting sort where the set of integers is sorted d - bits at a time . In order to achiave this we sort x [ i ] mod 2^ ( d * ( j - 1 )) for j = 1 to 64 / d . Smaller values of d will result in a slower sort ( more pases of the counting sort are required ) but will use less memory .
int RadixSortIndex (
    size_t *x,
    size_t *index,
    const size_t N,
    const size_t dshift,
    const size_t raw,
    const int verbose)
{
    int rc ;
    int i;
    size_t nloops  = 0;
    size_t exp   = 1;
    size_t max   = mf_max_unsigned(x, N);
    size_t min   = mf_min_unsigned(x, N);
    size_t range = max - min + 1;
    size_t ctol  = pow(2, 24);
    size_t shift; size_t loops;
    if (raw) {
        shift = dshift;
        loops = log(max) / log(dshift);
    }
    else {
        loops = 64 / dshift - 1;
        shift = pow(2, dshift);
    }
    for (i = 0; i < N; i++)
        index[i] = i;
    if ( range < ctol ) {
        if ( (rc = CountingSortIndex (x, index, N, min, max)) ) return(rc);
        if ( verbose ) {
            sf_printf("\t\tStep (2.1) counting sort: min = %'lu, max = %'lu\n", min, max);
        }
    }
    else {
        do {
            if ( (rc = RadixSortIndexPass (x, index, N, exp, shift)) ) return(rc);
        } while ( (max > (exp *= shift)) & (nloops++ < loops) );
        if ( verbose ) {
            sf_printf("\t\tStep (2.1) radix sort: loops = %lu, bits = %lu, shift = %'lu\n",
                      nloops, dshift, shift);
        }
    }
    return(0);
}
int RadixSortIndex ( size_t * x , size_t * index , const size_t N , const size_t dshift , const size_t raw , const int verbose ) { int rc ; int i ; size_t nloops = 0 ; size_t exp = 1 ; size_t max = mf_max_unsigned ( x , N ) ; size_t min = mf_min_unsigned ( x , N ) ; size_t range = max - min + 1 ; size_t ctol = pow ( 2 , 24 ) ; size_t shift ; size_t loops ; if ( raw ) { shift = dshift ; loops = log ( max ) / log ( dshift ) ; } else { loops = 64 / dshift - 1 ; shift = pow ( 2 , dshift ) ; } for ( i = 0 ; i < N ; i ++ ) index [ i ] = i ; if ( range < ctol ) { if ( ( rc = CountingSortIndex ( x , index , N , min , max ) ) ) return ( rc ) ; if ( verbose ) { sf_printf ( " \t \t \n " , min , max ) ; } } else { do { if ( ( rc = RadixSortIndexPass ( x , index , N , exp , shift ) ) ) return ( rc ) ; } while ( ( max > ( exp *= shift ) ) & ( nloops ++ < loops ) ) ; if ( verbose ) { sf_printf ( " \t \t \n " , nloops , dshift , shift ) ; } } return ( 0 ) ; }
@brief Radix sort on unsigned 64-bit integers with index
 Perform a radix sort on an array of 64-bit integers.
@brief Radix sort on unsigned 64 - bit integers with index Perform a radix sort on an array of 64 - bit integers .