基数排序

//此类排序只适合大整数数据,且位数相等,如1234,2221,3331,1000,3333等
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<time.h>
using namespace std;

template<class T>
class CMP //抽象操作符类
{
 public:
  virtual bool operator()(const T&one,const T&other)=0; //纯虚函数,重载操作符()
};

template<class T>
class LessThan:public CMP<T> //小于操作符类
{
 public:
  bool operator()(const T&one,const T&other)
  {
   return one<other;
  }
};

template<class T>
class GreatThan:public CMP<T>
{
 public:
  bool operator()(const T&one,const T&other)
  {
   return one>other;
  }
};

template<class Iterator,class T> //待排序元素的类型
class Sort //抽象排序类
{
 protected:
  Iterator array; //指向待排序区间的开始指针
 public:
  virtual void operator()(const Iterator &beg,const Iterator &end,CMP<T> &cmp)=0;
};

template<class Iterator,class T>
class RadixSort:public Sort<Iterator,T> //基数排序类
{
 private:
  Iterator assisted_array; //排序中辅助数组,用于存放排序好的元素
  Iterator counter_array; //排序中用来计数的数组
  int n; //待排序的元素个数
  void radix_sort();
  int maxbit; //最大的位数,以十进制为准
  int ValueofPos(int m,int k); //返回m从左数第k位上的数值
 public:
  RadixSort(int cnt)
  {
   maxbit=cnt;  
   assisted_array=new T[n];
   counter_array=new T[10]; //counter_array存放0到9之间每个元素出现的个数
   if(!assisted_array || !counter_array)
   {
    cout<<"内存空间不足!"<<endl;
    exit(0);
   }
  }
  void operator()(const Iterator &beg,const Iterator &end,CMP<T> &cmp)
  {
  }
  void operator()(const Iterator &beg,const Iterator &end);
  ~RadixSort()
  {
   try
   {
    delete assisted_array;
    delete counter_array;
   }
   catch(exception& e)
   {
    cerr<<e.what()<<endl;
   }
  }
};


template<class Iterator,class T>
void RadixSort<Iterator,T>::radix_sort()
{
 int i,j,index,pos;
 for(j=0;j<maxbit;j++)
 {
  for(i=0;i<10;i++) counter_array[i]=0; //将计数数组都置为0
  for(i=0;i<n;i++)
  {
   index=ValueofPos(array[i],j); /*找到索引 */ 
   counter_array[index]++; /*对该索引元素进行计数  */
  }
  for(i=1;i<10;i++) counter_array[i]+=counter_array[i-1]; //累计小于等于每个元素的个数
  for(i=n-1;i>=0;i--)
  {
   index=ValueofPos(array[i],j); 
   pos=counter_array[index]-1; /*在计数数组中找到array[i]应该在assisted_array数组中存放的位置*/
   assisted_array[pos]=array[i];  counter_array[index]--; 
  }
  for(i=0;i<n;i++) array[i]=assisted_array[i];
 }
}

template<class Iterator,class T>
void RadixSort<Iterator,T>::operator()(const Iterator &beg,const Iterator &end)
{
 n=end-beg;
 array=beg;
 radix_sort();
}

template<class Iterator,class T>
int RadixSort<Iterator,T>::ValueofPos(int m,int k)
{
 int i;
 int reminder,quotient; //reminder表示余数,quotient表示商
 int t;
 if(m==0) return 0;
 for(i=0;m && i<=k;i++)
 {
  reminder=m%10;
  quotient=m/10;
  m=m/10;
  t=reminder;
 }
 return t;
}

void main()
{
 //int b[]={2323,5456,6657,8139,5495,1338,3420,4934,1100,2233,2323,1101,8890,1000};
 vector<int>a;
 int x;
 srand((unsigned)time(0));
 const int n=1000;
 for(int i=0;i<n;i++)
 {
  x=rand()%n;
  if(x>=n/10) a.push_back(x);
  if(x>=n/10) cout<<x<<" ";
 }
 cout<<endl;
 RadixSort<vector<int>::iterator,int>sort(4);
 sort(a.begin(),a.end());
 cout<<"排序后的结果是:"<<endl;
 copy(a.begin(),a.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
}

你可能感兴趣的:(基数排序)