在C ++中实现Radix排序时出错

时间:2016-03-02 20:54:37

标签: c++ sorting radix-sort

我正在尝试使用long int数组实现Radix-Sort。

我的算法有2个输入:

  • 要排序的元素的数量“n”
  • 每位数字的数字“b”(因此,我记下取最后一个十进制数字,我取'b'数字组,代表一位数字)

但是,当我尝试对矢量进行排序时,我发现了两个问题:

  • 第一个:它没有正确地对矢量进行排序
  • 第二个:我已实现它从最后一组位到第一组。但似乎它试图向相反的方向排序。

以下是代码:

#define MAX_BITS sizeof(long int)*8
#define VectorL vector<long int>

struct Compare
{
   int beg = 0;
   int bits_per_digit = 2;

   Compare(int x, int y){
       beg = x;
       bits_per_digit = y;
   }

   bool operator() (long& a, long& b){
       int _begin = beg;
       int _final = _begin + bits_per_digit - 1;

       assert(_begin <= _final); // b==f if  bits_per_digit = 1;

       bitset<sizeof(long int)> _x(a), _y(b);

       for(_begin; _begin <= _final; _begin++)
           if(_x[_begin] != _y[_begin])
               return (_x[_begin] < _y[_begin]) ? true : false;

       return false;
   }
};

void ALG(VectorL& nums, int n, int b)
{
   assert(nums.size() == n);
   assert(MAX_BITS%b == 0);

   int _grupos = MAX_BITS/b;
   Compare myobject(MAX_BITS,b);

   for (int g = 1; g <= _grupos; g++)
   {
       myobject.beg -= b;

       sort(nums.begin(), nums.end(), myobject);
   }
}

这是主要的:

int n=5, b=2;
VectorL nums(n);
nums[0] = 2;
nums[1] = 6;
nums[2] = 1;
nums[3] = 5;
nums[4] = 0;

ALG(nums, n, b);

这是输出:

Initial Vector: 2 6 1 5 0
From bit 62 to bit 63
Vector after the 1º sort: 2 6 1 5 0
From bit 60 to bit 61
Vector after the 2º sort: 2 6 1 5 0
From bit 58 to bit 59
Vector after the 3º sort: 2 6 1 5 0
From bit 56 to bit 57
Vector after the 4º sort: 2 6 1 5 0
From bit 54 to bit 55
Vector after the 5º sort: 2 6 1 5 0
From bit 52 to bit 53
Vector after the 6º sort: 2 6 1 5 0
From bit 50 to bit 51
Vector after the 7º sort: 2 6 1 5 0
From bit 48 to bit 49
Vector after the 8º sort: 2 6 1 5 0
From bit 46 to bit 47
Vector after the 9º sort: 2 6 1 5 0
From bit 44 to bit 45
Vector after the 10º sort: 2 6 1 5 0
From bit 42 to bit 43
Vector after the 11º sort: 2 6 1 5 0
From bit 40 to bit 41
Vector after the 12º sort: 2 6 1 5 0
From bit 38 to bit 39
Vector after the 13º sort: 2 6 1 5 0
From bit 36 to bit 37
Vector after the 14º sort: 2 6 1 5 0
From bit 34 to bit 35
Vector after the 15º sort: 2 6 1 5 0
From bit 32 to bit 33
Vector after the 16º sort: 2 6 1 5 0
From bit 30 to bit 31
Vector after the 17º sort: 2 6 1 5 0
From bit 28 to bit 29
Vector after the 18º sort: 2 6 1 5 0
From bit 26 to bit 27
Vector after the 19º sort: 2 6 1 5 0
From bit 24 to bit 25
Vector after the 20º sort: 2 6 1 5 0
From bit 22 to bit 23
Vector after the 21º sort: 2 6 1 5 0
From bit 20 to bit 21
Vector after the 22º sort: 2 6 1 5 0
From bit 18 to bit 19
Vector after the 23º sort: 2 6 1 5 0
From bit 16 to bit 17
Vector after the 24º sort: 2 6 1 5 0
From bit 14 to bit 15
Vector after the 25º sort: 2 6 1 5 0
From bit 12 to bit 13
Vector after the 26º sort: 2 6 1 5 0
From bit 10 to bit 11
Vector after the 27º sort: 2 6 1 5 0
From bit 8 to bit 9
Vector after the 28º sort: 2 6 1 5 0
From bit 6 to bit 7
Vector after the 29º sort: 2 6 1 5 0
From bit 4 to bit 5
Vector after the 30º sort: 2 6 1 5 0
From bit 2 to bit 3
Vector after the 31º sort: 2 1 0 6 5
From bit 0 to bit 1
Vector after the 32º sort: 0 2 6 1 5

正如您在输出中看到的那样,更改仅发生在第31和第32类中(因此,这意味着在最后一次搜索中找到了有效位)。

任何人都可以帮我找到错误吗?

提前致谢。

0 个答案:

没有答案