随机化快速排序

本文介绍了一个通用的快速排序算法模板实现,使用C++编写,并通过继承实现不同比较方式的排序。该实现支持随机化选择枢轴,提高了算法的平均性能。

#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 QuickSort:public Sort<Iterator,T>
{
 private:
  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>
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[i]=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=88;
 int x;
 for(int i=0;i<n;i++)
 {
  x=rand()%n;
  a.push_back(x);
 }
 Quick_Sort<vector<int>::iterator,int>(a.begin(),a.end(),GreatThan<int>());
 //Merge_Sort<vector<int>::iterator,int>(a.begin(),a.end(),LessThan<int>());
 copy(a.begin(),a.end(),ostream_iterator<int>(cout," "));
 cout<<endl;
}

随机化快速排序是对经典快速排序的一种改进版本。它通过引入随机性来减少特定输入序列对算法效率的影响,特别是当输入已经接近有序的时候。核心思路是在每次划分之前不是固定选取某个位置的元素(如最后一位或第一位)作为基准值(pivot),而是随机从当前处理区间内选取出一个元素当作基准值。 以下是Java实现示例: ```java import java.util.Random; public class RandomizedQuickSort { public static void main(String[] args) { int[] arr = {5, 3, 8, 4, 2, 7, 1, 10}; randomizedQuicksort(arr, 0, arr.length - 1); for (int num : arr) { System.out.print(num + " "); } } // 主函数randomizedQuicksort public static void randomizedQuicksort(int[] nums, int start, int end) { if(start < end){ int pivotIndex = randomizedPartition(nums, start, end); randomizedQuicksort(nums, start, pivotIndex - 1); // 左侧继续快排 randomizedQuicksort(nums, pivotIndex + 1, end); // 右侧继续快排 } } // 随机选择并进行分区操作 private static int randomizedPartition(int[] nums, int p, int r){ Random rand = new Random(); int i = p + rand.nextInt(r-p+1); // 在 [p...r] 范围内生成一个随机索引 swap(nums, i, r); // 将选定的随机元素放到末尾 return partition(nums, p, r); // 执行常规的partion过程 } // 分区操作 private static int partition(int[] nums, int l, int h){ int x = nums[h]; int i = l - 1; for(int j=l; j<h; j++){ if(nums[j] <=x ){ i++; swap(nums, i, j); } } swap(nums, i+1, h); return i+1; } // 元素互换辅助函数 private static void swap(int[] a, int m, int n){ int temp = a[m]; a[m] = a[n]; a[n] = temp; } } ``` 上面代码展示了随机化快速排序的基本框架及其实现细节,并在`main()`方法里测试了该算法的效果。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值