几种排序算法的效率
对于排序算法的效率一直不太清楚,今天自己动手试了一试,才发现对于大量数据的排序时,各个算法效率的差异实在惊人!
代码如下:
Code
/********************************************************************
created: 2009/03/22
created: 22:3:2009 20:58
filename: e:\Graduate\Sort\Sort\Sort.cpp
file path: e:\Graduate\Sort\Sort
file base: Sort
file ext: cpp
author: zwicker
purpose: 用来演示几种排序的效率
*********************************************************************/
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
using namespace std;
//随机初始化数组
void initArray(int a[],int n)
{
srand(GetTickCount());
for (int i=0;i<n;i++)
{
a[i]=rand();
}
}
//插入排序
void insertionSort(int a[],int n)
{
for (int i=1;i<n;i++)
{
int tmp=a[i];
int j;
for (j=i;j>0&&tmp<a[j-1];j--)
{
a[j]=a[j-1];
}
a[j]=tmp;
}
}
//交换两个数
void swapValue(int &a,int &b)
{
int tmp=a;
a=b;
b=tmp;
}
//选择排序
void selectionSort(int a[],int n)
{
for(int past=0,j;past<n-1;past++)
{
int min=past;
for(j=past+1;j<n;j++)
{
if(a[j]<a[min])
min=j;
}
swapValue(a[past],a[min]);
}
}
//选择排序
void selectionSort2(int a[],int n)
{
int least,i,j;
for(i=0;i<n-1;i++)
for(j=i+1,least=i;j<n;j++)
if(a[j]<a[least])
least=j;
swapValue(a[least],a[i]);
}
//冒泡排序
void bubbleSort(int a[],int n)
{
for (int past=0;past<n-1;past++)
{
for (int j=n-1;j>past;j--)
{
if(a[j]<a[j-1])
swapValue(a[j],a[j-1]);
}
}
}
//打印数组
void printArray(int a[],int n)
{
int l=0;
for (int i=0;i<n;i++,l++)
{
if(l%10==0)
cout<<endl;
cout<<a[i]<<" ";
}
}
//希尔排序
void shellSort(int data[],int arrSize)
{
int i,j,hCnt,h;
int increaments[20],k;
for(h=1,i=0;h<arrSize;i++)
{
increaments[i]=h;
h=3*h+1;
}
for(i--;i>=0;i--)
{
h=increaments[i];
for(hCnt=h;hCnt<2*h;hCnt++)
{
for(j=hCnt;j<arrSize;)
{
int tmp=data[j];
k=j;
while (k-h>0&&tmp<data[k-h])
{
data[k]=data[k-h];
k-=h;
}
data[k]=tmp;
j+=h;
}
}
}
}
//判断数组是否有序
bool judgeSorted(int a[],int n)
{
for (int i=1;i<n-1;i++)
{
if(a[i]>a[i+1])
return false;
}
return true;
}
//快速排序
void quickSort(int data[],int first,int last)
{
int lower=first+1,upper=last;
swapValue(data[first],data[(first+last)/2]);
int bound=data[first];
while (lower<=upper)
{
while (data[lower]<bound)
lower++;
while(bound<data[upper])
upper--;
if(lower<upper)
swapValue(data[lower++],data[upper--]);
else
lower++;
}
swapValue(data[upper],data[first]);
if(first<upper-1)
quickSort(data,first,upper-1);
if(upper+1<last)
quickSort(data,upper+1,last);
}
void quickSort(int data[],int n)
{
if(n<2)
return;
int i,max;
for(i=1,max=0;i<n;i++)
if(data[max]<data[i])
max=i;
swapValue(data[n-1],data[max]);
quickSort(data,0,n-2);
}
int main()
{
int a[40000];//用于测试效率的数组
initArray(a,40000);//初始化数组
DWORD oldtime1=GetTickCount();//获得当前计算机Tick
insertionSort(a,40000);//进行插入排序
if(judgeSorted(a,40000))//判断数组是否已经排序
cout<<"The array has benn insertionSorted!"<<endl;
DWORD insertSortElapse=GetTickCount()-oldtime1;//获得插入排序的时间
initArray(a,40000);//重新初始化数组
DWORD oldtime2=GetTickCount();
selectionSort(a,40000);//进行选择排序
if(judgeSorted(a,40000))
cout<<"The array has benn selectionSorted!"<<endl;
DWORD selectSortElapse=GetTickCount()-oldtime2;
initArray(a,40000);
DWORD oldtime3=GetTickCount();
bubbleSort(a,40000);//进行冒泡排序
if(judgeSorted(a,40000))
cout<<"The array has benn bubbleSorted!"<<endl;
DWORD bubbleSortElapse=GetTickCount()-oldtime3;
initArray(a,40000);
DWORD oldtime4=GetTickCount();
shellSort(a,40000);//进行希尔排序
if(judgeSorted(a,40000))
cout<<"The array has been shellSorted!"<<endl;
DWORD shellSortElapse=GetTickCount()-oldtime4;
initArray(a,40000);
DWORD oldtime5=GetTickCount();
quickSort(a,40000);//进行快速排序
if(judgeSorted(a,40000))
cout<<"The array has been quickSorted!"<<endl;
DWORD qucikSortElapse=GetTickCount()-oldtime5;
//输出各个排序所耗费的时间
cout<<"InsertionSort Elapsed:"<<insertSortElapse<<endl;
cout<<"SelectionSort Elapsed:"<<selectSortElapse<<endl;
cout<<"BubbleSort Elapsed:"<<bubbleSortElapse<<endl;
cout<<"ShellSort Elapsed:"<<shellSortElapse<<endl;
cout<<"QuickSort Elapsed:"<<qucikSortElapse<<endl;
/*int a[]={2,3,5,8,1};
cout<<"Before InsertionSort:";
printArray(a,5);
cout<<endl<<"After InsertionSort:";
selectionSort(a,5);
printArray(a,5);
int b[5];
initArray(b,5);
cout<<endl<<"Before SelectionSort:";
printArray(b,5);
cout<<endl<<"After SelectionSort:";
selectionSort(b,5);
printArray(b,5);*/
//int c[]={2,3,5,8,1};
//cout<<endl<<"Before BubbleSort:";
//printArray(c,5);
//cout<<endl<<"After BubbleSort:";
//bubbleSort(c,5);
//printArray(c,5);
//int d[100];
//initArray(d,100);
//cout<<endl<<"Before ShellSort:";
//printArray(d,100);
//cout<<endl<<"After ShellSort:";
//shellSort(d,100);
//printArray(d,100);
int k;
cin>>k;
}
/********************************************************************
created: 2009/03/22
created: 22:3:2009 20:58
filename: e:\Graduate\Sort\Sort\Sort.cpp
file path: e:\Graduate\Sort\Sort
file base: Sort
file ext: cpp
author: zwicker
purpose: 用来演示几种排序的效率
*********************************************************************/
#include <iostream>
#include <stdlib.h>
#include <Windows.h>
using namespace std;
//随机初始化数组
void initArray(int a[],int n)
{
srand(GetTickCount());
for (int i=0;i<n;i++)
{
a[i]=rand();
}
}
//插入排序
void insertionSort(int a[],int n)
{
for (int i=1;i<n;i++)
{
int tmp=a[i];
int j;
for (j=i;j>0&&tmp<a[j-1];j--)
{
a[j]=a[j-1];
}
a[j]=tmp;
}
}
//交换两个数
void swapValue(int &a,int &b)
{
int tmp=a;
a=b;
b=tmp;
}
//选择排序
void selectionSort(int a[],int n)
{
for(int past=0,j;past<n-1;past++)
{
int min=past;
for(j=past+1;j<n;j++)
{
if(a[j]<a[min])
min=j;
}
swapValue(a[past],a[min]);
}
}
//选择排序
void selectionSort2(int a[],int n)
{
int least,i,j;
for(i=0;i<n-1;i++)
for(j=i+1,least=i;j<n;j++)
if(a[j]<a[least])
least=j;
swapValue(a[least],a[i]);
}
//冒泡排序
void bubbleSort(int a[],int n)
{
for (int past=0;past<n-1;past++)
{
for (int j=n-1;j>past;j--)
{
if(a[j]<a[j-1])
swapValue(a[j],a[j-1]);
}
}
}
//打印数组
void printArray(int a[],int n)
{
int l=0;
for (int i=0;i<n;i++,l++)
{
if(l%10==0)
cout<<endl;
cout<<a[i]<<" ";
}
}
//希尔排序
void shellSort(int data[],int arrSize)
{
int i,j,hCnt,h;
int increaments[20],k;
for(h=1,i=0;h<arrSize;i++)
{
increaments[i]=h;
h=3*h+1;
}
for(i--;i>=0;i--)
{
h=increaments[i];
for(hCnt=h;hCnt<2*h;hCnt++)
{
for(j=hCnt;j<arrSize;)
{
int tmp=data[j];
k=j;
while (k-h>0&&tmp<data[k-h])
{
data[k]=data[k-h];
k-=h;
}
data[k]=tmp;
j+=h;
}
}
}
}
//判断数组是否有序
bool judgeSorted(int a[],int n)
{
for (int i=1;i<n-1;i++)
{
if(a[i]>a[i+1])
return false;
}
return true;
}
//快速排序
void quickSort(int data[],int first,int last)
{
int lower=first+1,upper=last;
swapValue(data[first],data[(first+last)/2]);
int bound=data[first];
while (lower<=upper)
{
while (data[lower]<bound)
lower++;
while(bound<data[upper])
upper--;
if(lower<upper)
swapValue(data[lower++],data[upper--]);
else
lower++;
}
swapValue(data[upper],data[first]);
if(first<upper-1)
quickSort(data,first,upper-1);
if(upper+1<last)
quickSort(data,upper+1,last);
}
void quickSort(int data[],int n)
{
if(n<2)
return;
int i,max;
for(i=1,max=0;i<n;i++)
if(data[max]<data[i])
max=i;
swapValue(data[n-1],data[max]);
quickSort(data,0,n-2);
}
int main()
{
int a[40000];//用于测试效率的数组
initArray(a,40000);//初始化数组
DWORD oldtime1=GetTickCount();//获得当前计算机Tick
insertionSort(a,40000);//进行插入排序
if(judgeSorted(a,40000))//判断数组是否已经排序
cout<<"The array has benn insertionSorted!"<<endl;
DWORD insertSortElapse=GetTickCount()-oldtime1;//获得插入排序的时间
initArray(a,40000);//重新初始化数组
DWORD oldtime2=GetTickCount();
selectionSort(a,40000);//进行选择排序
if(judgeSorted(a,40000))
cout<<"The array has benn selectionSorted!"<<endl;
DWORD selectSortElapse=GetTickCount()-oldtime2;
initArray(a,40000);
DWORD oldtime3=GetTickCount();
bubbleSort(a,40000);//进行冒泡排序
if(judgeSorted(a,40000))
cout<<"The array has benn bubbleSorted!"<<endl;
DWORD bubbleSortElapse=GetTickCount()-oldtime3;
initArray(a,40000);
DWORD oldtime4=GetTickCount();
shellSort(a,40000);//进行希尔排序
if(judgeSorted(a,40000))
cout<<"The array has been shellSorted!"<<endl;
DWORD shellSortElapse=GetTickCount()-oldtime4;
initArray(a,40000);
DWORD oldtime5=GetTickCount();
quickSort(a,40000);//进行快速排序
if(judgeSorted(a,40000))
cout<<"The array has been quickSorted!"<<endl;
DWORD qucikSortElapse=GetTickCount()-oldtime5;
//输出各个排序所耗费的时间
cout<<"InsertionSort Elapsed:"<<insertSortElapse<<endl;
cout<<"SelectionSort Elapsed:"<<selectSortElapse<<endl;
cout<<"BubbleSort Elapsed:"<<bubbleSortElapse<<endl;
cout<<"ShellSort Elapsed:"<<shellSortElapse<<endl;
cout<<"QuickSort Elapsed:"<<qucikSortElapse<<endl;
/*int a[]={2,3,5,8,1};
cout<<"Before InsertionSort:";
printArray(a,5);
cout<<endl<<"After InsertionSort:";
selectionSort(a,5);
printArray(a,5);
int b[5];
initArray(b,5);
cout<<endl<<"Before SelectionSort:";
printArray(b,5);
cout<<endl<<"After SelectionSort:";
selectionSort(b,5);
printArray(b,5);*/
//int c[]={2,3,5,8,1};
//cout<<endl<<"Before BubbleSort:";
//printArray(c,5);
//cout<<endl<<"After BubbleSort:";
//bubbleSort(c,5);
//printArray(c,5);
//int d[100];
//initArray(d,100);
//cout<<endl<<"Before ShellSort:";
//printArray(d,100);
//cout<<endl<<"After ShellSort:";
//shellSort(d,100);
//printArray(d,100);
int k;
cin>>k;
}
这是运行结果:
The array has benn insertionSorted!
The array has benn selectionSorted!
The array has benn bubbleSorted!
The array has been shellSorted!
The array has been quickSorted!
InsertionSort Elapsed:3219
SelectionSort Elapsed:4344
BubbleSort Elapsed:18171
ShellSort Elapsed:32
QuickSort Elapsed:15
另一组测试数据:
The array has benn insertionSorted!
The array has benn selectionSorted!
The array has benn bubbleSorted!
The array has been shellSorted!
The array has been quickSorted!
InsertionSort Elapsed:2860
SelectionSort Elapsed:4125
BubbleSort Elapsed:17406
ShellSort Elapsed:16
QuickSort Elapsed:0
从运行结果可以看出对于大数据量的排序,快速排序效率比最慢的冒泡排序要快100多倍,多么惊人!