#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.