Radix Sort

I'm trying to implement radix sort into my program. Radix sort should take as input 2 parameters, RANGE and BASE. RANGE will be the highest value of integer that will be in the sorting data, and BASE will be an adjustable parameter that effects the efficiency of Radix sort.

This is what I have so far.

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
void radixSort(vector<int>& a, int base)
{
  int const MAX = 10;
  static vector<int> buckets[MAX];
  int pow = 1;
  
  for (int i = 0; i < MAX; ++i)
  {
    buckets[i].resize(a.size());
    buckets[i][0] = 0;
  }

  for (int i = 0; i < base; ++i, pow *= 10)
  {
    for (int j = 0; j < MAX; ++j)
    {
      buckets[j][0] = 0;
    }

    for (int j = 0; j < a.size(); ++j)
    {
      int index = (a[j] / pow) % 10;
      buckets[index][++buckets[index][0]] = a[j];
    }
      
    int cnt = 0;
    for (int j = 0; j < MAX; ++j)
    {
      for (int k = 0; k < buckets[j][0]; ++k)
      {
        a[cnt++] = buckets[j][k + 1];
      }
    }
  } 
}


I appreciate any help on this little problem.
Last edited on
Well it sorts when base is 9. I'm just not sure how to implement the base paremeter into my function so that it changes the efficiency of the program but still sort the numbers.
Topic archived. No new replies allowed.