6種排序的時間測試代碼

double* CreatArray(int len) //創建長度爲len的隨機數組
{
	int i;
	double* p=(double*)malloc(sizeof(double)*len);
	
	srand(time(0));
	for (i=0;i<len;i++)
	{
		p[i]=1000.0*rand()/(RAND_MAX+1.0);
	}
	return p;
}

void PrintArray(double* a,int len)
{
	for (int i=0;i<len;i++)
	{
		printf("%7.3f ",a[i]);
	}
	printf("\n");
}

void CheckArray(double* a,int len) //檢查數組中的值,是否是升序排列
{
	int i;
	for (i=1;i<len;i++)
	{
		if (a[i-1]>a[i])
			printf("error!\n");
	}
}

int cmpDouble(const void *x, const void *y)  //庫函數中的快速排序函數使用qsort
{  
	return (*(double*)x > *(double*)y ? 1 : -1);  
} 

int main(void)
{
	struct timeval tBegin,tEnd;
	double time=0;
	double *a=NULL;
	int len=50000*1000;

	/*
	a=CreatArray(len);
	gettimeofday(&tBegin);
	BubbleSort_1(a,0,len-1);
	gettimeofday(&tEnd);
	time=tEnd.tv_sec-tBegin.tv_sec+(tEnd.tv_usec-tBegin.tv_usec)/1000000.0;
	printf("冒泡 1 排序耗時%5.3lfs\n",time);
	CheckArray(a,len);
	free(a);

	a=CreatArray(len);
	gettimeofday(&tBegin);
	BubbleSort_2(a,0,len-1);
	gettimeofday(&tEnd);
	time=tEnd.tv_sec-tBegin.tv_sec+(tEnd.tv_usec-tBegin.tv_usec)/1000000.0;
	printf("冒泡 2 排序耗時%5.3lfs\n",time);
	CheckArray(a,len);
	free(a);

	a=CreatArray(len);
	gettimeofday(&tBegin);
	BubbleSort_3(a,0,len-1);
	gettimeofday(&tEnd);
	time=tEnd.tv_sec-tBegin.tv_sec+(tEnd.tv_usec-tBegin.tv_usec)/1000000.0;
	printf("冒泡 3 排序耗時%5.3lfs\n",time);
	CheckArray(a,len);
	free(a);


	a=CreatArray(len);
	gettimeofday(&tBegin);
	SelectSort(a,0,len-1);
	gettimeofday(&tEnd);
	time=tEnd.tv_sec-tBegin.tv_sec+(tEnd.tv_usec-tBegin.tv_usec)/1000000.0;
	printf("選擇排序耗時%5.3lfs\n",time);
	CheckArray(a,len);
	free(a);


	a=CreatArray(len);
	gettimeofday(&tBegin);
	InsertSort(a,0,len-1);
	gettimeofday(&tEnd);
	time=tEnd.tv_sec-tBegin.tv_sec+(tEnd.tv_usec-tBegin.tv_usec)/1000000.0;
	printf("插入排序耗時%5.3lfs\n",time);
	CheckArray(a,len);
	free(a);
*/



	a=CreatArray(len);
	gettimeofday(&tBegin);
	HeapSort(a,0,len-1);
	gettimeofday(&tEnd);
	time=tEnd.tv_sec-tBegin.tv_sec+(tEnd.tv_usec-tBegin.tv_usec)/1000000.0;
	printf("堆排序耗時%5.3lfs\n",time);
	CheckArray(a,len);
	free(a);

	
	a=CreatArray(len);
	gettimeofday(&tBegin);
	MergeSort_xunhuan(a,0,len-1);
	gettimeofday(&tEnd);
	time=tEnd.tv_sec-tBegin.tv_sec+(tEnd.tv_usec-tBegin.tv_usec)/1000000.0;
	printf("歸併排序耗時%5.3lfs\n",time);
	CheckArray(a,len);
	free(a);


	a=CreatArray(len);
	gettimeofday(&tBegin);
	qsort(a, len, sizeof(a[0]), cmpDouble); 
	gettimeofday(&tEnd);
	time=tEnd.tv_sec-tBegin.tv_sec+(tEnd.tv_usec-tBegin.tv_usec)/1000000.0;
	printf("庫函數快速排序耗時%5.3lfs\n",time);
	CheckArray(a,len);
	free(a);


	a=CreatArray(len);
	gettimeofday(&tBegin);
	QuickSort_2(a,0,len-1);
	gettimeofday(&tEnd);
	time=tEnd.tv_sec-tBegin.tv_sec+(tEnd.tv_usec-tBegin.tv_usec)/1000000.0;
	printf("快速排序耗時%5.3lfs\n",time);
	CheckArray(a,len);
	free(a);	
	

	return 0;
}


由於vs中沒有gettimeofday函數,下面是該函數的實現。

#ifndef UTC_TIME_STAMP_H_
#define UTC_TIME_STAMP_H_
#endif

#include <windows.h>
#include <sys/timeb.h>
#include <time.h>

#if !defined(_WINSOCK2API_) && !defined(_WINSOCKAPI_)
struct timeval
{
	long tv_sec;
	long tv_usec;
};
#endif

static int gettimeofday(struct timeval* tv)
{
	union {
		long long ns100;
		FILETIME ft;
	} now;
	GetSystemTimeAsFileTime (&now.ft);
	tv->tv_usec = (long) ((now.ns100 / 10LL) % 1000000LL);
	tv->tv_sec = (long) ((now.ns100 - 116444736000000000LL) / 10000000LL);

	return (0);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章