堆排序、快速排序(递归与非递归)、归并排序效率比较

堆排序、快速排序(递归与非递归)、归并排序效率比较

为了比较这四种排序的效率,程序中将四种排序算法封装成函数,并且随机生成10000000个随机数,用GetTickCount()函数获取精确到毫秒级的系统时间,通过这个方法,来比较效率。要注意的是递归方式的快速排序因为调用函数太多,所以效率很低,然后本人采用了STL库中的栈来非递归实现,结果效率更低,最后自己手写一个简易栈,效率就变得很高了。实验结果如图:
堆排序、快速排序(递归与非递归)、归并排序效率比较_第1张图片
代码如下:
#include
#include
#include
#include 
#define MAXN 10000000
using namespace std;
struct node//快速排序辅助栈中的结构体
{
	int low, high;	
};
struct 	Stack//快速排序辅助栈,这里采用自己写的简易栈,经测试比STL库中的栈速度快 
{
	node * base;
	node * top;
};
Stack InitStack()//初始化栈 
{
	Stack s;
	s.base = (node *)malloc(MAXN * sizeof(node));
	s.top = s.base;
	return s;	
}
void Push(Stack &s, node e)//入栈 
{
	*s.top = e;
	s.top++;	
}
node Pop(Stack &s)//出栈 
{
	node e = *s.top;
	s.top --;
	return e;
}
int Empty(Stack s)//判断栈是否为空 
{
	if(s.top == s.base)return 1;
	return 0;
}
void HeapAdjust(int L[], int s, int e)//堆排序的调整函数 
{
	int rc = L[s];
	for(int i = 2 * s; i <= e; i *= 2)
	{
		if(i < s && L[i] < L[i + 1]) i++;
		if(rc > L[i]) break;
		L[s] = L[i];
		s = i;
	} 
	L[s] = rc;
}
void HeapSort(int L[], int n)//堆排序 
{
	for(int i = n / 2; i > 0; i--)
		HeapAdjust(L, i, n);
	for(int i = n; i > 1; i--)
	{
		int temp = L[1];
		L[1] = L[i];
		L[i] = temp;
		HeapAdjust(L, 1, i - 1);
	}		
}
int Partition(int L[], int low, int high)//一趟快速排序,并返回中枢位置 
{
	L[0] = L[low];
	while(low < high)
	{
		while(low < high && L[high] >= L[0]) --high;
		L[low] = L[high];
		while(low < high && L[low] <= L[0]) ++low;
		L[high] = L[low];
	}
	L[low] = L[0];
	return low;
}
void QSort(int L[], int low, int high)
{
	if(low < high)
	{
		int k = Partition(L, low, high);
		QSort(L, low, k - 1);
		QSort(L, k + 1, high);
	}
}
void QuickSort_Rucursion(int L[], int low, int high)//递归实现快排
{
	QSort(L, low, high);
}
void QuickSort_Stack(int L[],int low, int high)//非递归实现快排 
{
	Stack s = InitStack();
	node no;
	int i;
	if(low < high)
	{
		i = Partition(L, low, high);
		if(low < i - 1)
		{
			no.low = low;
			no.high = i - 1;
			Push(s, no);
		}
		if(high > i + 1)
		{
			no.low = i + 1;
			no.high = high;
			Push(s, no);
		} 
		while(!Empty(s))
		{
			no = Pop(s);
			int l = no.low;
			int h = no.high;
			i = Partition(L, l, h);
			if(l < i - 1)
			{
				no.low = l;
				no.high = i - 1;
				Push(s, no);
			}
			if(h > i + 1)
			{
				no.low = i + 1;
				no.high = h;
				Push(s, no);
			} 
		}
	}
}
void Merge(int L[], int temp[],int first, int mid, int last)//归并操作 
{
	
	int i, j, k;
	for(i = first; i <= last; i++)
	{
		temp[i] = L[i];
	}
	
	for(i = first, j = mid + 1, k = i; i <= mid && j <= last;)
	{
		if(temp[i] > temp[j])
			L[k++] = temp[j++];
		else
			L[k++] = temp[i++];	
	}
	
	while(i <= mid)
		L[k++] = temp[i++];
	while(j <= last)
		L[k++] = temp[j++];		
}
void MSort(int L[], int temp[], int first, int last)
{
	if(L == NULL || temp == NULL)
		return ;
	if(first < last)
	{
		int mid = (first + last) / 2;
		MSort(L, temp, first, mid);
		MSort(L, temp, mid + 1, last);
		Merge(L, temp, first, mid, last); 
	}	
}
void MergeSort(int L[], int length)//递归实现归并排序 
{
	int * temp = (int *)malloc((length + 5) * sizeof(int));
	MSort(L, temp, 1, length);
}
int main()
{
	//生成10000000个随机数,并采用多种方式进行排序 
	int *L = (int *)malloc((MAXN + 1) * sizeof(int));
	DWORD t1,t2;
	srand(time(0));
	for(int i = 1; i <= MAXN; i++)
	{
		L[i] = rand();	 
	}
	t1 = GetTickCount(); 
	HeapSort(L, MAXN);
	t2 = GetTickCount();
	printf("堆排序消耗时间为 %d 毫秒\n", t2 - t1);
	t1 = GetTickCount();
	QuickSort_Rucursion(L, 1, MAXN);
	t2 = GetTickCount();
	printf("递归快速排序消耗时间为 %d 毫秒\n", t2 - t1);
	t1 = GetTickCount();
	QuickSort_Stack(L, 1, MAXN);
	t2 = GetTickCount();
	printf("非递归快速排序消耗时间为 %d 毫秒\n", t2 - t1);
	t1 = GetTickCount();
	MergeSort(L, MAXN);
	t2 = GetTickCount();
	printf("递归归并排序消耗时间为 %d 毫秒\n", t2 - t1);
	return 0;
} 


你可能感兴趣的:(数据结构)