选择排序、快速排序、希尔排序、堆排序不是稳定的排序算法
冒泡排序、插入排序、归并排序和基数排序都是稳定的排序算法。
各种算法的稳定性分析:http://www.cnblogs.com/Braveliu/archive/2013/01/15/2861201.html
总结:
(1)如果数据量非常小,那么适合用简单的排序算法:冒泡排序,选择排序和插入排序。因为他们虽然比较次数多,但是移动次数少。比如,如果记录的关键字本身信息量比较大(比如关键字都是数十位的数字),那么占用存储空间比较大,这样移动记录所花费的时间也就很多。因此,应该尽量少移动。
(2)如果数据量非常大,那么就用快排。快排可以对pivot的选择进行优化,一般优化方式是三数取中,即左端,右端,中间三个数取中值。
(3)如果快排表现不好,也就时由于选择pivot的原因,排序朝着二次幂的方向发展,那么就采用堆排序。
(4)SGI STL中的Sort()函数就是采用的上述方式。
(5)堆排序对空间的要求很小,如果软件执行环境非常在乎内存,那么请选择堆排序。
(6)堆排序和归并排序都是最好,最坏,平均情况的效率都是O(nlogn),如果程序非常在乎稳定性请选择归并排序。
0X00.冒泡排序
typedef struct SqList
{
<span style="white-space:pre"> </span>int r[MAXSIZE+1];
<span style="white-space:pre"> </span>int length;
}SqList;
void swap(SqList *L,int i,int j)
{
int temp=L->r[i];
L->r[i]=L->r[j];
L->r[j]=temp;
}
void BubbleSort(SqList *L)
{
for(int i=1;i<L->length;i++)
{
for(int j=L->length;j>i;j--)
{
if(L->r[j]<L->r[j-1])
swap(L,j,j-1); //swap(L,j,j-1);
}
}
}
0X01.选择排序
void SelectSort(SqList *L)
{
for(int i=1;i<L->length;i++)
{
int min=i;
for(int j=i+1;j<=L->length;j++)
{
if(L->r[j]<L->r[min])
min=j;
}
if(min!=i)
swap(L,i,min);
}
}
0X02、插入排序(INSERTION-SORT)
#include <iostream>
#include <stdlib.h>
using namespace std;
//插入排序
void insertion_sort(int a[],unsigned int first,unsigned int last)
{
int j,i;
int key;
for(j=first+1;j<=last;j++)
{
key=a[j];
i=j-1;
while(i>=first&&key<a[i])
{
a[i+1]=a[i];
i=i-1;
}
a[i+1]=key;
}
}
//打印数组
void printnumber(int a[],int count)
{
int i;
for(i=0;i<count;i++)
cout<<a[i]<<" ";
cout<<endl;
}
int main()
{
int count;
cout<<"Please input the num of numbers:"<<endl;
cin>>count;
while(count<=0)
{
cout<<"There should be more than 0 numbers!"<<endl;
cout<<"Please input again:"<<endl;
cin>>count;
}
int *a=new int [count];
cout<<"Please input the "<<count<<" numbers:"<<endl;
int i,num;
for(i=0;i<count;i++)
{
cin>>num;
*(a+i)=num;
}
cout<<"The numbers before sorted:"<<endl;
printfnumber(a,count);
insertion_sort(a,0,count-1);
cout<<"The numbers after sorted:"<<endl;
printnumber(a,count);
delete []a;
system("pause");
}
0X03、归并排序(MERGE-SORT)
#include <iostream>
#include <stdlib.h>
using namespace std;
//A[p...q;q+1...r]
void merge(int A[],int p,int q,int r)
{
int i,j,k,n1,n2;
//n1,n2分别用来记录两个数组的数据个数
n1=q-p+1;
n2=r-q;
//数组的第一个位置没有用来存放数据
int *L=new int[n1+2];
int *R=new int[n2+2];
//分别用L,R来记录两个待排序数组
for(i=1;i<=n1;i++)
L[i]=A[p+i-1];
for(j=1;j<=n2;j++)
R[j]=A[q+j];
//将两个数组的最后数值都设置为较大值:10000
L[n1+1]=10000;
R[n2+1]=10000;
i=j=1;
//将L,R两个数组中较小者先放入数组A中
for(k=p;k<=r;k++)
{
if(L[i]<=R[j])
{
A[k]=L[i];
i++;
}
else
{
A[k]=R[j];
j++;
}
}
delete []L;
delete []R;
}
//归并排序
void merge_sort(int A[],int p,int r)
{
if(p<r)
{
int q;
q=(p+r)/2;
merge_sort(A,p,q);
merge_sort(A,q+1,r);
merge(A,p,q,r);
}
}
//打印数组
void printnumber(int A[],int count)
{
int i;
for(i=0;i<count;i++)
cout<<*(A+i)<<" ";
}
void main()
{
cout<<"MERGE-SORT is running!"<<endl;
cout<<"Please input the quantity of the numbers:"<<endl;
int count;
cin>>count;
while(count<=0)
{
cout<<"There should be more than 0 numbers!"<<endl;
cout<<"Please re-input again:"<<endl;
cin>>count;
}
cout<<"Please input the "<<count<<"numbers:"<<endl;
int i;
int *A=new int[count];
for(i=0;i<count;i++)
cin>>*(A+i);
cout<<"The numbers before sorted:"<<endl;
printnumbers(A,count);
cout<<"MERGE-SORT-ing..."<<endl;
merge_sort(A,0,count-1);
cout<<"The numbers after sorted:"<<endl;
printnumber(A,count);
delete []A;
system("pause");
}
0X04、快速排序(快排、QUICK-SORT)
这是算法导论上的思路:
#include <iostream>
#include <stdlib.h>
using namespace std;
//分组
int PARTITION(int A[],int p,int r)
{
int i,j,x,temp;
x=A[r];
i=p-1;
for(j=p;j<=r-1;j++)
{
if(A[j]<=x)
{
i++;
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
}
temp=A[i+1];
A[i+1]=A[r];
A[r]=temp;
return i+1;
}
//快排
void QUICK_SORT(int A[],int p,int r)
{
if(p<r)
{
int q;
q=PARTITION(A,p,r);
QUICK_SORT(A,p,q-1);
QUICK_SORT(A,q+1,r);
}
}
//打印数组
void printnumber(int A[],int count)
{
int i;
for(i=0;i<count;i++)
cout<<*(A+i)<<" ";
cout<<endl;
}
void main()
{
cout<<"QUICK-SORT is running!"<<endl;
cout<<"Please input the quantity of the numbers:"<<endl;
int count;
cin>>count;
while(count<=0)
{
cout<<"There should be more than 0 numbers!"<<endl;
cout<<"Please re-input the quantity:"<<endl;
cin>>count;
}
cout<<"Please input the "<<count<<"numbers:"<<endl;
int i;
int *A=new int[count];
//A[0...count-1]
for(i=0;i<count;i++)
cin>>*(A+i);
cout<<"The numbers before sorted:"<<endl;
printnumber(A,count);
QUICK_SORT(A,0,count-1);
cout<<"The numbers after sorted:"<<endl;
printnumber(A,count);
system("pause");
}
这是一般的思路:
int Partition(SqList *L,int low,int high)
{
int pivotkey=L->r[low];
while(low<high)
{
while(low<high&&L->r[high]>pivotkey)
high--;
swap(L,low,high);
while(low<high&&L->r[low]<pivotkey)
low++;
swap(L,low,high);
}
return low;
}
void QSort(SqList *L,int low,int high)
{
int pivot;
if(low<high)
{
pivot=Partition(L,low,high);
QSort(L,low,pivot-1);
QSort(L,pivot+1,high);
}
}
0X05、STOOGE排序(STOOGE-SORT)
#include <iostream>
#include <stdio.h>
using namespace std;
//STOOGE排序
void STOOGE_SORT(int A[],int i,int j)
{
if(A[i]>A[j])
{
int temp;
temp=A[i];
A[i]=A[j];
A[j]=temp;
}
if(i+1>=j)
return;
int k=(j-i+1)/3;
STOOGE_SORT(A,i,j-k);
STOOGE_SORT(A,i+k,j);
STOOGE_SORT(A,i,j-k);
}
//打印数组
void printnumber(int A[],int count)
{
int i;
for(i=0;i<count;i++)
cout<<*(A+i)<<" ";
cout<<endl;
}
void main()
{
cout<<"STOOGE-SORT is running!"<<endl;
cout<<"Please input the quantity of the numbers:"<<endl;
int count;
cin>>count;
while(count<=0)
{
cout<<"There should be more than 0 numbers!"<<endl;
cout<<"Please re-input the quantity:"<<endl;
cin>>count;
}
cout<<"Please input the "<<count<<" numbers:"<<endl;
int i;
int *A=new int[count];
for(i=0;i<count;i++)
cin>>*(A+i);
cout<<"The numbers before sorted:"<<endl;
printnumber(A,count);
cout<<"STOOGE-SORT-ing..."<<endl;
STOOGE_SORT(A,0,count-1);
cout<<"The numbers after sorted:"<<endl;
printnumber(A,count);
system("pause");
}
0X06、堆排序(HEAP-SORT)
利用最大堆。
#include <iostream>
#include <stdio.h>
using namespace std;
//内联函数,用于计算父亲节点、左孩子节点、右孩子节点
inline int PARENT(int i){return i/2;}
inline int LEFT(int i){return 2*i;}
inline int RIGHT(int i){return 2*i+1;}
//说明:A[1,..,heapsize],数组A的A[0]不用,实际,length(A)=heapsize+1。
//该函数用于保持堆的性质
void MAX_HEAPIFY(int A[],int i,int heapsize)
{
int l,r,temp,largest=0;
l=LEFT(i);
r=RIGHT(i);
//找到A[i],A[l],A[r]三者中的最大值
if(l<=heapsize&&A[l]>A[i])
largest=l;
else
largest=i;
if(r<=heapsize&&A[r]>A[largest])
largest=r;
//如果拥有最大值的不是根节点i,那么将A[i]和A[largest]进行交换
//并递归调用MAX_HEAPIFY()对孩子节点进行处理
if(largest!=i)
{
temp=A[i];
A[i]=A[largest];
A[largest]=temp;
MAX_HEAPIFY(A,largest,heapsize);
}
}
//该函数用于构建最大堆
void BUILD_MAX_HEAP(int A[],int heapsize)
{
int i;
for(i=heapsize/2;i>=1;i--)
MAX_HEAPIFY(A,i,heapsize);
}
//堆排序
void HEAP_SORT(int A[],int length)
{
//首先构建最大堆
BUILD_MAX_HEAP(A,length);
int i,temp,heapsize=length;
for(i=heapsize;i>=2;i--)
{
//将最大的A[1]和末尾的A[i]交换,即:将当前堆中的最大值移到末尾
temp=A[1];
A[1]=A[i];
A[i]=temp;
//堆的大小减一
heapsize--;
//对根节点进行调整,即:重新构建当前的最大堆
MAX_HEAPIFY(A,1,heapsize);
}
}
//用于打印数组的函数
void printnumber(int A[],int heapsize)
{
int i;
for(i=1;i<=heapsize;i++)
cout<<*(A+i)<<" ";
cout<<endl;
}
void main()
{
cout<<"MAX-HEAP-SORT is running"<<endl;
cout<<"Please input the quantity of the numbers:"<<endl;
//要求用户输入要输入的堆的大小,此处,数组的长度length=堆的大小heapsize+1
int count;
cin>>count;
while(count<=0)
{
cout<<"There should be more than 0 numbers!"<<endl;
cout<<"Please re-input the quantity:"<<endl;
cin>>count;
}
//用户输入数组
cout<<"Please input the "<<count<<" numbers:"<<endl;
int i;
int *A=new int[count+1];
//A[1,..,count],heapsize=count-1+1=count;
for(i=0;i<count;i++)
cin>>*(A+i+1);
//进行堆排序
cout<<"The numbers before sorted:"<<endl;
printnumber(A,count);
HEAP_SORT(A,count);
cout<<"The numbers after sorted:"<<endl;
printnumber(A,count);
delete []A;
system("pause");
}
0X07.ShellSort(希尔排序)
void ShellSort(SqList *L)
{
int i,j,increment=L->length;
increment=increment/3+1;
bool flag=true;
while(increment>=1&&flag)
{
if(increment==1)
flag=false;
for(i=increment+1;i<=L->length;i++)
{
L->r[0]=L->r[i];
for(j=i-increment;j>=1&&L->r[j]>L->r[0];j-=increment)
{
L->r[j+increment]=L->r[j];
}
L->r[j+increment]=L->r[0];
}
increment=increment/3+1;
}
}
0X08.总结
0X09.其他排序
此外,还有其他排序方法包括:桶排序,计数排序,基数排序。
桶排序和计数排序基本一样,一个数组分别统计每个数出现的个数。
基数排序是先对个位数排序,然后对十位数排序,然后对百位数排序,以此类推。