The*_*iot 5

这里有一个来自wikibooks(这是最低有效数字)

public void RadixSort(int[] a)
{  
    // our helper array 
    int[] t=new int[a.Length]; 

    // number of bits our group will be long 
    int r=4; // try to set this also to 2, 8 or 16 to see if it is 
             // quicker or not 

    // number of bits of a C# int 
    int b=32; 

    // counting and prefix arrays
    // (note dimensions 2^r which is the number of all possible values of a 
    // r-bit number) 
    int[] count=new int[1<<r]; 
    int[] pref=new int[1<<r]; 

    // number of groups 
    int groups=(int)Math.Ceiling((double)b/(double)r); 

    // the mask to identify groups 
    int mask = (1<<r)-1; 

    // the algorithm: 
    for (int c=0, shift=0; c<groups; c++, shift+=r)
    { 
        // reset count array 
        for (int j=0; j<count.Length; j++)
            count[j]=0;

        // counting elements of the c-th group 
        for (int i=0; i<a.Length; i++)
            count[(a[i]>>shift)&mask]++; 

        // calculating prefixes 
        pref[0]=0; 
        for (int i=1; i<count.Length; i++)
            pref[i]=pref[i-1]+count[i-1]; 

        // from a[] to t[] elements ordered by c-th group 
        for (int i=0; i<a.Length; i++)
            t[pref[(a[i]>>shift)&mask]++]=a[i]; 

        // a[]=t[] and start again until the last group 
        t.CopyTo(a,0); 
    } 
    // a is sorted 
}
Run Code Online (Sandbox Code Playgroud)

  • 这不是关于你的答案的评论,而是关于那篇wiki的文章.一本学习算法的书包含几乎无法读取和理解的代码.只是说) (3认同)
  • @abatishchev,老实说,对我来说,它乍一看是可读的......它只是说明你想要增加所需组的计数器......变量移位是掩码的大小,所以你本质上是在移动通过你的号码的面具并通过这种方式获得组 ID,我猜.. (3认同)