1. 程式人生 > >排序演算法的C++實現與效能分析(插入排序、歸併排序、快速排序、STOOGE排序、堆排序)

排序演算法的C++實現與效能分析(插入排序、歸併排序、快速排序、STOOGE排序、堆排序)

選擇排序、快速排序、希爾排序、堆排序不是穩定的排序演算法

氣泡排序、插入排序、歸併排序和基數排序都是穩定的排序演算法。

總結:

(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.其他排序

此外,還有其他排序方法包括:桶排序,計數排序,基數排序。

桶排序和計數排序基本一樣,一個數組分別統計每個數出現的個數。

基數排序是先對個位數排序,然後對十位數排序,然後對百位數排序,以此類推。