基数排序

描述:将数依次按个位,十位,百位。。。排序

// support function for radixSort()
// distribute vector elements into one of 10 queues
// using the digit corresponding to power
//   power = 1    ==> 1's digit
//   power = 10   ==> 10's digit
//   power = 100  ==> 100's digit
//   ...
//将数据按个位(power=1),十位(power=10)。。。排序并存入10个队列中
void distribute(const vector<int>& v, queue<int> digitQueue[],
                int power)
{
 int i;

 // loop through the vector, inserting each element into
 // the queue (v[i] / power) % 10
 for (i = 0; i < v.size(); i++)
  digitQueue[(v[i] / power) % 10].push(v[i]);
}

// support function for radixSort()
// gather elements from the queues and copy back to the vector
//将10个队列中的数据收集起来放到一个向量中
void collect(queue<int> digitQueue[], vector<int>& v)
{
 int i = 0, digit;

 // scan the vector of queues using indices 0, 1, 2, etc.
 for (digit = 0; digit < 10; digit++)
  // collect items until queue empty and copy items back
  // to the vector
  while (!digitQueue[digit].empty())
  {
   v[i] = digitQueue[digit].front();
   digitQueue[digit].pop();
   i++;
  }
}

void radixSort(vector<int>& v, int d)
{
 int i;
 int power = 1;
 queue<int> digitQueue[10];

 for (i=0;i < d;i++)
 {
  distribute(v, digitQueue, power);
  collect(digitQueue, v);
  power *= 10;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值