决策树、记数排序、基数排序

//决策树 O(nlgn)
const order[][3] = {
    {0,1,2},{0,2,1},{1,0,2},{1,2,0},{2,0,1},{2,1,0},
};
int descide_tree_sort(int a1, int a2, int a3;)
{
    if (a1<a2) {
        if (a2<a3)   return 0; //out( a1<a2<a3 );
        else {
            if (a1<a3) return 1; //out( a1<a3<=a2 );
            else       return 4; //out( a3<=a1<a2 );
        }
    }
    else {
        if (a1<a3)   return 2; //out( a2<=a1<a3 );
        else {
            if (a2<a3) return 3; //out( a2<a3<=a1 );
            else       return 5; //out( a3<=a2<=a1 );
        }
    }
    return -1;
}

//记数排序
/*
 * 数组A[0...N-1]中的元素都小于K,
 * 数组C[0...K-1]中用于记数,C[i]表示数组A[]中小于i的元素个数。
 * 数组O[0...N-1]用于输出。
 * */
void counting_sort(int A[], int n, int C[], int k, int O[])
{
    for (int i=0; i<k; i++)
        C[i] = 0;
    for (int i=0; i<n; i++)
        C[A[i]]++; // C[i] 表示等于i的元素个数。
    for (int i=1; i<k; i++)
        C[i] += C[i-1]; // C[i] 表示小于等于i的元素个数。
    for (int i=n-1; i>=0; i--) {
        O[ C[A[i]] ] = A[i];
        C[A[i]]--; // 下一个等于A[i]的元素直接放入在C[]中的A[i]的前一个位置。
    }
}

//基数排序
//根据数值的位进行排序。比如四位数,依次根据数值的个位,
//十位,百位,千位数字使用稳定排序。
// 如:
/*
320     702     720     329
457     355     329     355
657     436     436     436
839  => 457  => 839  => 457
436     657     355     657
720     329     457     720
355     839     657     839
          ^       ^       ^
*/
/* R 表示基数
 * d = lg( max(A[]) )。
 * */
#define R 16
void radix_sort(int A[], int n, int d)
{
    for (int i=0; i<d; i++)
        stable_sort(A, n, i);
}
原文地址:https://www.cnblogs.com/prajna/p/2940556.html