算法导论7-2,针对相同元素的快速排序

#include <iostream>
#include <ctime>
#include <utility>
#include <boost/timer/timer.hpp>
using namespace boost::timer;
using namespace std;


void same_elem_quick_sort(int *a,int low,int high)//考虑相同元素的情况
{
	if(low<high){
		int pivot=a[low];
		int i=low,h=low;
		for(int j=low+1;j<high;++j){
			if(a[j]<pivot){
				int y=a[j];
				a[j]=a[h+1];
				a[h+1]=a[i];
				a[i]=y;
				++h,++i;
			}else if(a[j]==pivot){
				int y=a[j];
				a[j]=a[h+1];
				a[h+1]=y;
				++h;
			}
		}
		same_elem_quick_sort(a,low,i);
		same_elem_quick_sort(a,h+1,high);
	}

}



void same_elem_quick_sort2(int *a,int low,int high)//考虑相同元素的情况,小的放在pivot左边,
{                                                 //大的放在pivot右边,相等的不变,最后相等的就可以连在一起
	if(low<high){
		int pivot=a[low];
		int i=low-1;
		int k=high;
		for(int j=low+1;j<k;++j){
			if(a[j]<pivot){
				++i;
				int y=a[j];				
				a[j]=a[i];
				a[i]=y;
			}else if(a[j]>pivot){
				--k;
				int y=a[k];
				a[k]=a[j];
				a[j]=y;
				--j;
			}
		}
		//if(i-low<high-h){
			same_elem_quick_sort(a,low,i);
			//low=h+1;
		//}else{
			same_elem_quick_sort(a,k,high);
			//high=i;
		//}
	}

}


int main()
{
	srand(time(NULL));
	int count;
	while((count=(rand()%30))<10);
	count=100000000;  
	int *a=new int[count];
	int *b=new int[count];
	for(int i=0;i<count;++i){
		a[i]=rand()%100000001-50000000;  
		b[i]=a[i];		
		//cout<<a[i]<<" ";
	}
	//cout<<endl;

	cpu_timer t1;
	t1.start();
	same_elem_quick_sort(a,0,count);
	t1.stop();
	//for(int i=0;i<count;++i){
	//	cout<<a[i]<<" ";
	//} 
	//cout<<endl;
	cout<<t1.format();

	cpu_timer t2;
	t2.start();
	same_elem_quick_sort2(b,0,count);
	t2.stop();
	/*for(int i=0;i<count;++i){
		cout<<b[i]<<" ";
	}
	cout<<endl;*/
	cout<<t2.format();

	delete[]a;
	delete[] b;
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值