快速排序(递归,非递归),希尔排序,冒泡排序的比较

3 篇文章 0 订阅
3 篇文章 0 订阅

数据量:100w整型数组

代码如下:

#include <iostream>
#include <stack>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;

template <typename Comparable>
int partition(vector<Comparable> &vec, int low, int high)
{
	Comparable pivot = vec[low];
	while(low<high)
	{
		while(low<high && vec[high]>=pivot)
			high--;
		vec[low] = vec[high];

		while(low<high && vec[low]<=pivot)
			low++;
		vec[high] = vec[low];
	}
	vec[low] = pivot;
	return low;
}
template <typename Comparable>
void quickSortRecursion(vector<Comparable> &vec, int low, int high)
{
	if(low>=high)return ;

	int mid = partition(vec, low, high);
	quickSortRecursion(vec, low, mid-1);
	quickSortRecursion(vec, mid+1, high);
}
template <typename Comparable>
void quickSortNonRecursion(vector<Comparable> &vec, int low, int high)
{
	if(low>=high)return ;
	stack<int> st;
	int mid = partition(vec, low, high);
	if(low<mid-1)
	{
		st.push(low);
		st.push(mid-1);
	}
	if(mid+1<high)
	{
		st.push(mid+1);
		st.push(high);
	}
	while(!st.empty())
	{
		high = st.top();
		st.pop();
		low = st.top();
		st.pop();
		mid = partition(vec, low, high);
		if(low<mid-1)
		{
			st.push(low);
			st.push(mid-1);
		}
		if(mid+1<high)
		{
			st.push(mid+1);
			st.push(high);
		}
	}
}
template <typename Comparable>
void bubble(vector<Comparable> &vec)
{
	int len = vec.size();
	//vector<int> vec;
	Comparable tmp;
	for(int i=0; i<len; i++)
	{
		for(int j=i+1; j<len; j++)
		{
			if(vec.at(i)>vec.at(j))
			{
				tmp = vec.at(i);
				vec.at(i) = vec.at(j);
				vec.at(j) = tmp;
			}
		}
	}
}

template <typename Comparable>
void shellSort(vector<Comparable> &vec)
{	
	int len = vec.size();
	int grap = 0;
	int i,j;
	while(grap<len)
	{
		grap = grap*3 + 1;
	}
	while(grap>0)
	{
		for(i=grap; i<len; i++)
		{
			Comparable tmp = vec.at(i);
			j = i - grap;
			while(j>=0 && vec.at(j)>tmp)
			{
				vec.at(j+grap) = vec.at(j);
				j -= grap;
			}
			vec.at(j+grap) = tmp;
		}
		grap = (grap-1)/3;
	}

}

int main()
{
	int len = 1000000;
	vector<int> vec;
	
	int i;
	for(i=0; i<len; i++)
		vec.push_back(rand());


	clock_t t1 = clock();
	quickSortRecursion(vec, 0, len-1);
	clock_t t2 = clock();

	cout<<"quick sort Recursive :"<<1.0*(t2-t1)/CLOCKS_PER_SEC<<endl;

	random_shuffle(vec.begin(), vec.end());
	
	t1 = clock();
	quickSortNonRecursion(vec, 0, len-1);
	t2 = clock();

	cout<<"quick sort None Recursive :"<<1.0*(t2-t1)/CLOCKS_PER_SEC<<endl;

	random_shuffle(vec.begin(), vec.end());

	t1 = clock();
	shellSort(vec);
	t2 = clock();

	cout<<"shellSort :"<<1.0*(t2-t1)/CLOCKS_PER_SEC<<endl;

	random_shuffle(vec.begin(), vec.end());

	t1 = clock();
	bubble(vec);
	t2 = clock();

	cout<<"Bubble :"<<1.0*(t2-t1)/CLOCKS_PER_SEC<<endl;

	cout<<"End"<<endl;
	return 0;
}


测试结果(时间s):

linux,Intel Xoen CPU 2.4GHz,Mem 2G

quick sort Recursive :1.66

quick sort None Recursive :1.76

shellSort :14.73

Bubble :unknown

Win7,Intel Pentium Dual 2GHz,Mem 2G

quick sort Recursive :7.529

quick sort None Recursive :14.569

shellSort :552.917

Bubble :unknown



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值