radixsort.cpp

// Radix sort
// 
// Simplified code as example for the radix sort algorithm.
//
// by feyd//godX.de
 
#include <vector>
#include <stdio.h>
 
// Base class, radix sort
template<class _TYPE, int _BITS>
class CRadixSort
{
public:
    CRadixSort() : m_histogram(1<<_BITS) {}
    virtual ~CRadixSort() {}
 
    const std::vector<_TYPE>& Sort(const std::vector<_TYPE>& toSort)
    {
        const std::vector<_TYPE>* pInput = &toSort;
        std::vector<_TYPE>* pOutput = &m_output1;
 
        int nRound = 0;
        for(int nBitPos=0; nBitPos<sizeof(_TYPE)*8; nBitPos+=_BITS)
        {
            // Clear counters
            for(size_t n=0; n<m_histogram.size(); n++)
                m_histogram[n] = 0;
 
            // Count equal number parts
            for(size_t n=0; n<pInput->size(); n++)
                m_histogram[((*pInput)[n]>>nBitPos)&((1<<_BITS)-1)]++;
 
            // Build base positions
            size_t nBase = 0;
            for(size_t n=0; n<m_histogram.size(); n++)
            {
                size_t nCount = m_histogram[n];
                m_histogram[n] = nBase;
                nBase += nCount;
            }
 
            // Reorder
            pOutput->resize(pInput->size());
            for(size_t n=0; n<pInput->size(); n++)
            {
                _TYPE nValue = (*pInput)[n];
                size_t nPos = (nValue>>nBitPos)&((1<<_BITS)-1);
                (*pOutput)[m_histogram[nPos]] = nValue;
                m_histogram[nPos]++;
            }
 
            // Swap buffers
            if(nRound&1)
            {
                pInput = &m_output2;
                pOutput = &m_output1;
            } else {
                pInput = &m_output1;
                pOutput = &m_output2;
            }
            nRound++;
        }   
        return *pInput;
    }
 
    std::vector<size_t> m_histogram;
    std::vector<_TYPE> m_output1;
    std::vector<_TYPE> m_output2;
};
 
// Application entry point (from libc)
int main(int argc, const char* argv[])
{
    printf("Radix sort test...\r\n\r\n");
 
    std::vector<int> arrayToSort;
    for(int n=1; n<argc; n++)
        arrayToSort.push_back(atoi(argv[n]));
 
    CRadixSort<int, 8> sort;
    const std::vector<int>& output = sort.Sort(arrayToSort);
 
    printf("   sorted %d elements:\r\n", (int)output.size());
    for(size_t n=0; n<output.size(); n++)
        printf("      %d. %d\r\n", (int)n+1, output[n]);
    printf("\r\n");
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89