为什么这个radixSort不起作用

时间:2014-04-13 00:12:21

标签: c++ radix-sort

它看起来很不错。但结果却说不同的东西。真的,我不知道它有什么不对。也许是因为我是在晚上2点钟写的......

编辑:

template <class T>
void radixSort(T & arr, msize numberBase)
{
    long maxValue = findMax(arr);
    dynarray<long> tarr(arr,arr.size());
    dynarray<long> presenceTable(numberBase+1);
    for (register long i=0, max=presenceTable.size(); i<max; ++i)
    {
        presenceTable[i] = 0;
    }
    for (register long exp=1; maxValue/exp>0; exp*=numberBase)
    {
        for (register long i=0, max=tarr.size(); i<max; ++i)
        {
             ++(presenceTable[(tarr[i]/exp)%numberBase]);
        }
        for (register long i=1, max=presenceTable.size(); i<max; ++i)
        {
            presenceTable[i] += presenceTable[i-1];
        }
        for (register long i=0, max=tarr.size(); i<max; ++i)
        {
            arr[  (--(presenceTable[(tarr[i]/exp)%numberBase]))  ] = tarr[i];
        }
        tarr = arr;
        for (register long i=0, max=presenceTable.size(); i<max; ++i)
        {
        presenceTable[i] = 0;
        }
    }
}

0 个答案:

没有答案