随机化快速次序选择算法

#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
#include<time.h>
#include<stdlib.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 QuickSort:public Sort<Iterator,T>
{
 protected:
  int low,high; //指向待排序区间开始和结束
  int Partition(int low,int high,CMP<T>&cmp); //将区间[low...high]划分成[low...pivot],[pivot+1...high]
  int RandPos(int low,int high); //在low...high之间返回一个随机的位置
  void Exchange(int i,int j); //交换位置i和位置j的两个元素
  void Sort_Function(int low,int high,CMP<T>&cmp); //将区间[low...high]归并排序
 public:
  void operator()(const Iterator &beg,const Iterator &end,CMP<T> &cmp)
  {
   low=0;
   high=end-beg-1;
   array=beg;
   Sort_Function(low,high,cmp);
  }
};


template<class Iterator,class T>
class SelectKMin:public QuickSort<Iterator,T> //随机选择第k个最小元素
{
 private:
  int k; //第k个最小
  T kmin; //第k个最小元素
  void selectkmin(int start,int end,int k,CMP<T>&cmp); //在start和end之间选择第k个最小元素

 public:
  SelectKMin(int i):k(i)
  {
  }
  void operator()(const Iterator& beg,const Iterator &end,CMP<T> &cmp);
  T getKMin() const
  {
   return kmin;
  }
};

template<class Iterator,class T>
void SelectKMin<Iterator,T>::selectkmin(int start,int end,int k,CMP<T>&cmp) //在start和end之间选择第k个最小元素
{
 int pivot,i;
 if(start==end) //找到
 {
  kmin=array[start];
  return;
 }
 pivot=Partition(start,end,cmp);
 i=pivot-start; //i表示第i小元素,从0开始算起
 if(i==k) //已经找到
 {
  kmin=array[pivot]; //此时中枢元素就是第k个最小元素
  return;
 }
 else if(k<i) selectkmin(start,pivot-1,k,cmp);
 else selectkmin(pivot+1,end,k-i-1,cmp); //k-i-1表示在新的区间要查找第k-i-1个最小元素
}

template<class Iterator,class T>
void SelectKMin<Iterator,T>::operator()(const Iterator& beg,const Iterator &end,CMP<T> &cmp)
{
 low=0;
 high=end-beg-1;
 array=beg;
 if(k<0 || k>high)
 {
  cerr<<"构造参数不合法,应该在0到"<<high<<"之间!"<<endl;
  exit(0);
 }
 selectkmin(low,high,k,cmp);
}

template<class Iterator,class T>
int QuickSort<Iterator,T>::RandPos(int low,int high)
{
 int n=high-low+1;
 srand((unsigned)time(0));
 int offset=rand()%n;
 if(offset==n) offset--;
 int pos=low+offset;
 if(pos<=high && pos>=low) return pos;
 else return low;
}

template<class Iterator,class T>
void QuickSort<Iterator,T>::Exchange(int i,int j)
{
 T tmp;
 tmp=array[i];
 array[i]=array[j];
 array[j]=tmp;
}

template<class Iterator,class T>
int QuickSort<Iterator,T>::Partition(int low,int high,CMP<T> &cmp)
{
 int pos=RandPos(low,high);
 Exchange(low,pos);
 int i,j;
 i=low;
 j=high;
 T tmp=array[low];
 while(i<j)
 {
  while(i<j && array[j]>tmp) j--;
  if(i<j) array[i++]=array[j];
  while(i<j && array[i]<tmp) i++;
  if(i<j) array[j--]=array[i];
 }
 array[i]=tmp;
 return i;
}

template<class Iterator,class T>
void QuickSort<Iterator,T>::Sort_Function(int low,int high,CMP<T> &cmp)
{
 if(low<high)
 {
  int pivot=Partition(low,high,cmp);
  Sort_Function(low,pivot-1,cmp);
  Sort_Function(pivot+1,high,cmp);
 }
}
template<class Iterator,class T>
void Quick_Sort(const Iterator &beg,const Iterator &end,CMP<T>&cmp)
{
 QuickSort<Iterator,T> ms;
 ms(beg,end,cmp);
}

void main()
{
 vector<int>a;
 srand((unsigned)time(0));
 const int n=10;
 int x;
 for(int i=0;i<n;i++)
 {
  x=rand()%n;
  a.push_back(x);
  cout<<x<<" ";
 }
 cout<<endl;
 int cnt=0;
 int k;
 do
 {
  cout<<"请选择要查找第几个最小元素:";
  cin>>k;
  SelectKMin<vector<int>::iterator,int>skm(k);
  skm(a.begin(),a.end(),LessThan<int>());
  cout<<"第"<<k<<"个最小元素是:"<<skm.getKMin()<<endl;
  cnt++;
 }while(cnt<n);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值