选择,插入,交换,冒泡,希尔排序算法的效率比较

#include<iostream>
#include<time.h>
using namespace std;
template<class T>
void bubbleSort(T arr[],int n)
{
	int temp,i,j;
	bool flag=false;
	for(i=0;i<n;i++)//控制趟数
	{
		flag=false;
		for(j=1;j<n;j++)
			if(arr[j-1]>arr[j])
			{
				temp=arr[j];
				arr[j]=arr[j-1];
				arr[j-1]=temp;
				flag=true;
			}
			if(!flag)
				break;
	}
}
void xuanzeSort(int arr[],int n)
{
	int smallIndex;
	int pass,j;
	int temp;
	for(pass=0;pass<n-1;pass++)
	{
		smallIndex=pass;
		for(j=pass+1;j<n;j++)
			if(arr[j]<arr[smallIndex])
				smallIndex=j;
		if(smallIndex!=pass)
		{
			temp=arr[pass];
			arr[pass]=arr[smallIndex];
			arr[smallIndex]=temp;
		}
	}
}
template<class T>
void swapSort(T arr[],int n)
{
	int i,j,temp;
	for(i=0;i<n;i++)
	{
		for(j=i+1;j<n;j++)
		{
			if(arr[j]<arr[i])
			{
				temp=arr[i];
				arr[i]=arr[j];
				arr[j]=temp;
			}
		}
	}
}
template<class T>
void shellSort(T arr[],int n)
{
	int i,j,gap=n;
	T temp;
	do
	{
		gap=gap/3+1;//间隔
		for(i=gap;i<n;i++)
			if(arr[i]<arr[i-gap])
			{
				temp=arr[i];
				j=i-gap;
				do
				{
					arr[j+gap]=arr[j];
					j=j-gap;
				}
				while(j>=0&&temp<arr[j]);
				arr[j+gap]=temp;//插入
			}
	}
	while(gap>1);//间隔
}
template<class T>
void insertionSort(T arr[],int n)
{
	int i,j;
	T target;
	for(i=1;i<n;i++)
	{
		j=i;
		target=arr[i];
		while(j>0&&target<arr[j-1])
		{
			arr[j]=arr[j-1];
			j--;
		}
		arr[j]=target;
	}
}
int main()
{
	clock_t  star,end;
	const int arr_size=10000;
	int i,list1[arr_size],list2[arr_size],list3[arr_size],list4[arr_size],list5[arr_size];
	srand(time(NULL));
	for(int j=0;j<3;j++)
	{
		switch(j)
		{
		case 0:
			cout<<endl<<"随机数"<<endl;
			for(i=0;i<arr_size;i++)//随机数
				list1[i]=list2[i]=list3[i]=list4[i]=list5[i]=rand()%arr_size;
			break;
		case 1:
			cout<<endl<<"正序数"<<endl;
			for(i=0;i<arr_size;i++)//正序数
				list1[i]=list2[i]=list3[i]=list4[i]=list5[i]=i;
			break;
		case 2:
			cout<<endl<<"倒序数"<<endl;
			for(i=0;i<arr_size;i++)//倒序数
				list1[i]=list2[i]=list3[i]=list4[i]=list5[i]=arr_size-i;
			break;
		}
	for(int i=0;i<arr_size;i++)//随机数
		list1[i]=list2[i]=list3[i]=list4[i]=list5[i]=rand()%arr_size;
	cout<<"Timing the Selection Sort"<<endl;
	star=clock();
	xuanzeSort(list1,arr_size);
	end=clock();
	cout<<"Selection Sort takes "<<end-star<<" seconds."<<endl;
	 
	cout<<"Timing the Bubble Sort"<<endl;
	star=clock();
	xuanzeSort(list2,arr_size);
	end=clock();
	cout<<"Bubble Sort takes "<<end-star<<" seconds."<<endl;

	cout<<"Timing the Insert Sort"<<endl;
	star=clock();
	insertionSort(list3,arr_size);
	end=clock();
	cout<<"Insert Sort takes "<<end-star<<" seconds."<<endl;

	cout<<"Timing the Swap Sort"<<endl;
	star=clock();
	swapSort(list4,arr_size);
	end=clock();
	cout<<"Swap Sort takes "<<end-star<<" seconds."<<endl;

	cout<<"Timing the Shell Sort"<<endl;
	star=clock();
	shellSort(list5,arr_size);
	end=clock();
	cout<<"Shell Sort takes "<<end-star<<" seconds."<<endl;
	}
	return 0;
}


对10000个数据排序的时间效率结果,最好是希尔排序,最差是交换排序,最好最坏情况的时间效率相差不大,从这里可以看出为什么算法宁愿找个最差情况比较好的算法也不要平均情况比较好的算法.

随机数
Timing the Selection Sort
Selection Sort takes 130 seconds.
Timing the Bubble Sort
Bubble Sort takes 130 seconds.
Timing the Insert Sort
Insert Sort takes 60 seconds.
Timing the Swap Sort
Swap Sort takes 280 seconds.
Timing the Shell Sort
Shell Sort takes 10 seconds.

正序数
Timing the Selection Sort
Selection Sort takes 120 seconds.
Timing the Bubble Sort
Bubble Sort takes 120 seconds.
Timing the Insert Sort
Insert Sort takes 60 seconds.
Timing the Swap Sort
Swap Sort takes 290 seconds.
Timing the Shell Sort
Shell Sort takes 10 seconds.

倒序数
Timing the Selection Sort
Selection Sort takes 120 seconds.
Timing the Bubble Sort
Bubble Sort takes 120 seconds.
Timing the Insert Sort
Insert Sort takes 60 seconds.
Timing the Swap Sort
Swap Sort takes 290 seconds.
Timing the Shell Sort
Shell Sort takes 0 seconds.

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值