日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

排序算法的性能比较

發布時間:2024/1/21 windows 40 coder
生活随笔 收集整理的這篇文章主要介紹了 排序算法的性能比较 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫在前面

菜雞博主最近放寒假了,打算接下來的一段時間內回顧一下以前學過的一些知識,也是為考研做一些準備吧。很菜,勿噴,希望能和大家一起學習,共同進步!

主要目標和具體要求

目標

排序對于數據處理是一項十分重要和基礎的工作。采用如下算法實現排序功能:插入排序、交換排序、選擇排序、歸并排序、基數排序等。統計每種排序算法的性能(以相同計算機運行程序所花費的時間為準進行對比),并將數據序列和不同算法的運行時間記錄到txt文件中。

要求

設計排序系統,能夠實現以下功能:

(1) 利用隨機函數產生N個隨機整數(50000以上);

(2) 對這些數字進行排序,包括遞增和遞減兩種形式;

(3) 分別采用插入、折半插入、2-路插入、希爾、冒泡、快速、選擇、錦標賽、堆、歸并、基數等排序算法解決問題;

(4) 對不同的排序算法進行性能比較并記錄結果。

環境

操作系統:Windows 11

編譯器:visual studio 2022

使用的頭文件:iostream, algorithm, ctime, cstdlib, cstring, fsteam

一些設計

1.Random函數

作用:生成范圍在l-r內的隨機數。

首先通過srand(time(0))設置時間種子,然后通過循環和語句rand()%(r-l+1)+1生成隨機數,隨機數的范圍為1-32767。

2.save函數

作用:文件操作

首先定義flag標記換行,然后打開文件,如果不存在文件則新建,通過time()記錄所用的時間,通過for循環輸出排序結果(其中flag換行是通過每記錄一個數據使flag自加,當flag==100時使用endl語句換行,同時重置flag=0),在輸出完所有內容后關閉文件。

3.直接插入排序

將第一個元素看作是一個有序序列,后面n-1個元素看作無序序列,對后面未排序序列中第一個元素在這個有序序列中進行從后向前的掃描,找到合適的位置進行插入,將有序序列長度+1。循環操作,知道未排序序列長度為0。當前插入元素與有序序列中元素相等時,插入到相等元素后。

4.折半插入排序

運用二分查找,定義temp來記錄當前待插元素在有序序列中的合適位置,對temp進行后移,在temp位置插入待插元素。

5.2-路插入排序

2-路插入排序是對插入排序的改進,通過在首尾同時進行插入,使用臨時數組tmp,定義front和rear記錄tmp數組中當前的最大值和最小值位置。如果當前插入元素比最小的要小,更新最小值位置,并且將最小值重新賦值;如果當前插入元素比最大的要大,更新最大值位置,并且將最大值重新賦值;如果在最大和最小之間,將比當前插入值大的進行后移,然后插入,同時更新最大值位置,最后將臨時數組復制到原數組中。

6.希爾排序

選擇一個增量序列\(t_1t_2 \dots t_k\),其中\(t_i > t_j, t_k =1\);按照增量序列個數k對序列進行k趟排序;每趟排序根據對應的增量![img](file:///C:/Users/wyy/AppData/Local/Temp/msohtmlclip1/01/clip_image004.png),將待排序列分割成若干長度為m的子序列,分別對各子表進行直接插入排序。僅增量因子為1時,整個序列作為一個表來處理,表長度即整個序列的長度。

7.快速排序

先從數列中取出一個數作為基準數;分區過程:將比這個數大的數全放到它的右邊,小于或等于它的數全放到它的左邊;對左右兩個區間重復第二步直到各區間只有一個數。本算法使用三數取中的方法取基準值:取序列中最左邊、中間、最右邊的三個數進行比較,以這三個數大小為中的元素作為基準值。

8.選擇排序

將比當前未排序序列的首位元素較小的數據元素交換到首位,最后每一趟比較都會將當前最小的元素交換到為排序序列的首位。改進后,通過定義min來確定未排序序列中的最小元素位置。

9.錦標賽排序

樹形選擇排序,由于其特殊性質,錦標賽排序構成的圖結構是滿二叉樹。定義一個tree[]數組來儲存滿二叉樹,錄入葉子節點(需要排序的所有元素),因為二叉樹某結點的下標為i,它的左右孩子節點下標必為2i+1和2i+2,因此比較葉子節點的值,可以得到父節點的值(兩個葉子節點的值較小的那個),每次得到當前最小值的時候,通過minindex在tree[]中找到最小值的地址,將其設為MAX。

10.堆排序

使用大根堆對數組進行正向排序。

首先將當前的無序數組構成一個無序堆,某結點的下標為i,它的左右孩子節點下標必為2i+1和2i+2,然后將當前的無序堆進行調整至大根堆形成。接著交換首位元素(將當前堆頂的最大元素交換到末尾,將此元素歸位。因為交換完首位元素后堆會再次變成一個無序堆,需要對剩余元素進行調整至重新變成大根堆。)重復操作至堆的大小為1完成堆排序。

11.歸并排序

將長度為n的序列分解成兩個長度為n/2的子序列,遞歸排序兩個連續子序列,合并兩個已經排序的連續子序列構成一個完整排序的序列。

12.基數排序

定義一個count[]數組(大小為10,下標0-9)用于統計每一位數字出現的次數,一個大小為n的臨時數組tmp[]用于儲存按照當前以為數字排序后的序列。進行d次排序,每次分配前先清空計數器,從低位開始,統計每一位數字出現的個數記錄到count[]中,由遞推式\(start[i]=start[i-1]+count[i-1]\)得到每一位數字第一次出現的位置。然后將每個元素按照當前的這一位數字放入對應的0-9的桶中,每一次將所有元素放入對應桶中后,根據start[]將其放到臨時數組tmp[],再將其拷貝到原數組中。重復上述步驟,當按照最高位放入桶中的操作完成后,再放回原數組。

13.計時

使用clock_t庫,定義start和end,將不同的排序算法寫在start和end之間,定義duration=(double)(end-start)/CLOCKS_PER_SEC來輸出需要的時間并打印。

源代碼

#include<iostream>
#include<algorithm>
#include<ctime>
#include<time.h>
#include<cstdlib>
#include<cstring> 
#include<fstream>
using namespace std;

#define random(x) rand() % (x)


void save(int a[], string str, double time)
{
	int flag = 0;//標記換行
	fstream f;
	f.open("data.txt", ios::out | ios::app);
	f << str << "所用的時間:" << time << "s" << endl;
	for (int i = 0; i < 50000; i++)
	{
		flag++;
		f << a[i] << "-";
		if (flag == 100)
		{
			f << endl;
			flag = 0;
		}
	}
	f << endl << endl;
	f.close();
}

//生成范圍在l~r的隨機數
void Random(int* a, int n, int l, int r)
{
	srand(time(0));//設置時間種子
	for (int i = 0; i < n; i++)
	{
		a[i] = rand() % (r - l + 1) + 1;//生成隨機數
	}
}

//打印(測試用)
void Print(int* a, int n)
{
	for (int i = 0; i < n; i++)
		cout << a[i] << " ";
	cout << endl;
}

//直接插入排序(正向)
void InsertSort_up(int a[], int n)
{
	int temp, i, j;
	for (i = 1; i < n; i++)
	{
		if (a[i] < a[i - 1])
		{
			temp = a[i];
			a[i] = a[i - 1];
			for (j = i - 2; j >= 0 && a[j] > temp; --j)
			{
				a[j + 1] = a[j];
			}
			a[j + 1] = temp;
		}
	}
}

//直接插入排序(逆向)
void InsertSort_down(int a[], int n)
{
	int temp, i, j;
	for (int i = 0; i < n - 1; i++)
	{
		int end = i + 1;
		int temp = a[end];
		if (a[end] > a[i])
		{
			while (end > 0 && temp > a[end - 1])
			{
				a[end] = a[end - 1];
				end--;
			}
		}
		a[end] = temp;
	}
}

//折半插入排序(正向)
void BinaryInsertionSort_up(int a[], int left, int right)
{
	int i, j;
	int mid, temp;
	i = left, j = right;
	mid = a[(left + right) >> 1];
	do
	{
		while (a[i] < mid && (i < right))
			i++;
		while (a[j] > mid && (j > left))
			j--;
		if (i <= j)
		{
			temp = a[i];
			a[i] = a[j];
			a[j] = temp;
			i++;
			j--;
		}
	} while (i <= j);
	if (left < j)
		BinaryInsertionSort_up(a, left, j);
	if (right > i)
		BinaryInsertionSort_up(a, i, right);
}

//折半插入排序(逆向)
void BinaryInsertionSort_down(int  a[], int n)
{
	BinaryInsertionSort_up(a, 0, n - 1);
	reverse(&a[0], &a[50001]);
}

//2-路插入排序(正向)
void TwoInsertSort_up(int a[], int n)
{
	int* tmp = new int[n];     //臨時數組
	int front = 0, rear = 0;   //記錄當前tmp數組中最大值和最小值的位置
	tmp[0] = a[0];             //初始化tmp

	for (int i = 1; i < n; i++)
	{
		int key = a[i];
		//如果當前插入的元素比最小的元素更小
		if (key < tmp[front])
		{
			front = (front - 1 + n) % n;
			tmp[front] = key;
		}
		//如果當前插入元素比最大元素更大
		else if (key > tmp[rear])
		{
			rear = (rear + 1 + n) % n;
			tmp[rear] = key;
		}
		//如果在當前最小和最大之間
		else
		{
			int k = (rear + n) % n;
			//將比當前插入值key大的進行后移
			while (tmp[(k + n) % n] > key)
			{
				tmp[(k + 1 + n) % n] = tmp[(k + n) % n];
				k = (k - 1 + n) % n;
			}
			tmp[(k + 1 + n) % n] = key; //當前插入值放到合適位置
			rear = (rear + 1 + n) % n;  //更新最大值位置(有序序列長度+1)
		}
	}

	//復制臨時數組到原數組中
	for (int k = 0; k < n; k++)
		a[k] = tmp[(front + k) % n];
	delete[] tmp;
}

//2-路插入排序(逆向)
void TwoInsertSort_down(int a[], int n)
{
	int* tmp = new int[n];     //臨時數組
	int front = 0, rear = 0;   //記錄當前tmp數組中最大值和最小值的位置
	tmp[0] = a[0];             //初始化tmp

	for (int i = 1; i < n; i++)
	{
		int key = a[i];
		//如果當前插入的元素比最小的元素更小
		if (key < tmp[front])
		{
			front = (front - 1 + n) % n;
			tmp[front] = key;
		}
		//如果當前插入元素比最大元素更大
		else if (key > tmp[rear])
		{
			rear = (rear + 1 + n) % n;
			tmp[rear] = key;
		}
		//如果在當前最小和最大之間
		else
		{
			int k = (rear + n) % n;
			//將比當前插入值key大的進行后移
			while (tmp[(k + n) % n] > key)
			{
				tmp[(k + 1 + n) % n] = tmp[(k + n) % n];
				k = (k - 1 + n) % n;
			}
			tmp[(k + 1 + n) % n] = key; //當前插入值放到合適位置
			rear = (rear + 1 + n) % n;  //更新最大值位置(有序序列長度+1)
		}
	}

	//復制臨時數組到原數組中
	for (int k = 0; k < n; k++)
		a[k] = tmp[(front + k) % n];
	reverse(&a[0], &a[50001]);//倒序
	delete[] tmp;
}

//希爾排序(正向)
void ShellSort_up(int a[], int n)
{
	int temp = n;//初始化增量
	while (temp > 1)//最后一次增量為1
	{
		temp = temp / 3 + 1;
		for (int i = temp; i < n; i++)
		{
			int key = a[i];//當前需要插入的數
			int j = i - temp;
			while (j >= 0 && a[j] > key)
			{
				a[j + temp] = a[j];
				j -= temp;
			}
			a[j + temp] = key;
		}
	}
}

//希爾排序(逆向)
void ShellSort_down(int a[], int n)
{
	int temp = n;//初始化增量
	while (temp > 1)//最后一次增量為1
	{
		temp = temp / 3 + 1;
		for (int i = temp; i < n; i++)
		{
			int key = a[i];//當前需要插入的數
			int j = i - temp;
			while (j >= 0 && a[j] > key)
			{
				a[j + temp] = a[j];
				j -= temp;
			}
			a[j + temp] = key;
		}
	}
	reverse(&a[0], &a[50001]);
}

//快速排序(正向)
int GetMid(int a[], int left, int right)
{
	int mid = (left + right) >> 1;
	if (a[left] < a[mid])
		if (a[mid] < a[right])
			return mid;
		else
			if (a[left] < a[right])
				return right;
			else
				return left;
	else
		if (a[mid] > a[right])
			return right;
		else
			return left;
}

int PartitionBetter(int a[], int left, int right)
{
	int pos = GetMid(a, left, right);
	swap(a[pos], a[left]);
	int i = left;
	int j = right;
	int key = a[left];
	while (i != j)
	{
		while (i < j && a[j] >= key)
			j--;
		while (i < j && a[i] <= key)
			i++;
		swap(a[i], a[j]);
	}
	swap(a[i], a[left]);
	return i;
}

void QuickSort_up(int a[], int left, int right)
{
	if (left >= right)
		return;
	int i = PartitionBetter(a, left, right);
	QuickSort_up(a, left, i - 1);
	QuickSort_up(a, i + 1, right);
}

//快速排序(逆向)
void QuickSort_down(int a[], int n)
{
	QuickSort_up(a, 0, n - 1);
	reverse(&a[0], &a[50001]);
}

//選擇排序(正向)
void SelectSort_up(int a[], int n)
{
	for (int i = 0; i < n; i++)
	{
		int min = i;
		for (int j = i + 1; j < n; j++)
		{
			if (a[min] > a[j])
				min = j;
		}
		swap(a[i], a[min]);
	}
}

//選擇排序(逆向)
void SelectSort_down(int a[], int n)
{
	SelectSort_up(a, n);
	reverse(&a[0], &a[50001]);
}

//錦標賽排序(正向)
void TreeSelectSort_up(int a[], int n)
{
	int sum = n * 2 - 1;//滿二叉樹節點總數
	int* tree = new int[sum];
	//輸入葉子節點
	for (int i = n - 1, j = 0; i >= 0; i--, j++)
		tree[sum - j - 1] = a[i];
	//非葉子節點
	for (int i = sum - n - 1; i >= 0; i--)
		tree[i] = tree[2 * i + 1] < tree[2 * i + 2] ? tree[2 * i + 1] : tree[2 * i + 2];
	int k = 0;
	int minindex = -1;
	while (k < n)
	{
		int min = tree[0];
		a[k++] = min;
		minindex = sum - 1;
		//從最后葉子節點開始找到最小值的位置,將其設置為MAXIUM
		while (tree[minindex] != min)
			minindex--;
		tree[minindex] = INT_MAX;
		//若該節點有父節點,將其兄弟結點提升為父節點
		while (minindex > 0)
		{
			if (minindex % 2 == 1)
			{
				tree[(minindex - 1) / 2] = tree[minindex] < tree[minindex + 1] ? tree[minindex] : tree[minindex + 1];
				minindex = (minindex - 1) / 2;
			}
			else
			{
				tree[minindex / 2 - 1] = tree[minindex] < tree[minindex - 1] ? tree[minindex] : tree[minindex - 1];
				minindex = minindex / 2 - 1;
			}
		}
	}
	delete[] tree;
}

//錦標賽排序(逆向)
void TreeSelectSort_down(int a[], int n)
{
	TreeSelectSort_up(a, n);
	reverse(&a[0], &a[50001]);
}

//堆排序(正向)
void Heapify(int a[], int i, int n)
{
	int left = 2 * i + 1, right = 2 * i + 2;//二叉樹當前節點的左右節點
	int max = i;//默認i為最大值
	if (left < n && a[left]>a[max])
		max = left;
	if (right <n && a[right]>a[max])
		max = right;
	if (max != i)
	{
		//調整最大值到非葉子節點處
		swap(a[i], a[max]);
		//互換后,子節點變化,若子節點有其子節點仍需調整,遞歸
		Heapify(a, max, n);
	}
}

void HeapSort_up(int a[], int n)
{
	for (int i = n / 2 - 1; i >= 0; i--)//構建大根堆
		Heapify(a, i, n);
	while (n > 1)
	{
		swap(a[0], a[n - 1]);//交換首位數據,尾部最大
		Heapify(a, 0, --n);//重置大根堆
	}
}

//堆排序(逆向)
void HeapSort_down(int a[], int n)
{
	HeapSort_up(a, n);
	reverse(&a[0], &a[50001]);
}

//歸并排序(正向)
void Merge(int a[], int left, int mid, int right)
{
	int* temp = new int[right - left + 1];
	int i = left, j = mid + 1, k = 0;
	while (i <= mid && j <= right)
	{
		if (a[i] < a[j])
		{
			temp[k] = a[i];
			i++;
		}
		else
		{
			temp[k] = a[j];
			j++;
		}
		k++;
	}
	while (i <= mid)
	{
		temp[k] = a[i];
		i++;
		k++;
	}
	while (j <= right)
	{
		temp[k] = a[j];
		j++;
		k++;
	}
	i = left;
	for (int tempK = 0; ((tempK < k) && (i <= right)); tempK++)
	{
		a[i] = temp[tempK];
		i++;
	}
	delete[] temp;
	temp = NULL;
}

void MergeSort_up(int a[], int left, int right)
{
	if (left < right)
	{
		int mid = (left + right) >> 1;
		MergeSort_up(a, left, mid);
		MergeSort_up(a, mid + 1, right);
		Merge(a, left, mid, right);
	}
}

//歸并排序(逆向)
void MergeSort_down(int a[], int n)
{
	MergeSort_up(a, 0, 50000);
	reverse(&a[0], &a[50001]);
}

//基數排序(正向)
//求數據的最大位數, 決定排序次數
int maxbit(int a[], int n)
{
	int d = 1; //保存最大的位數
	int p = 10;
	for (int i = 0; i < n; ++i)
	{
		while (a[i] >= p)
		{
			p *= 10;
			++d;
		}
	}
	return d;
}

void RadixSort_up(int a[], int n) //基數排序
{
	int d = maxbit(a, n);
	int tmp[50001];
	int count[10]; //計數器
	int i, j, k;
	int radix = 1;
	for (i = 1; i <= d; i++) //進行d次排序
	{
		for (j = 0; j < 10; j++)
			count[j] = 0; //每次分配前清空計數器
		for (j = 0; j < n; j++)
		{
			k = (a[j] / radix) % 10; //統計每個桶中的記錄數
			count[k]++;
		}
		for (j = 1; j < 10; j++)
			count[j] = count[j - 1] + count[j]; //將tmp中的位置依次分配給每個桶
		for (j = n - 1; j >= 0; j--) //將所有桶中記錄依次收集到tmp中
		{
			k = (a[j] / radix) % 10;
			tmp[count[k] - 1] = a[j];
			count[k]--;
		}
		for (j = 0; j < n; j++) //將臨時數組的內容復制到a中
			a[j] = tmp[j];
		radix = radix * 10;
	}
}

//基數排序(逆向)
void RadixSort_down(int a[], int n)
{
	RadixSort_up(a, n);
	reverse(&a[0], &a[50001]);
}


int main()
{
	srand(time(0));
	clock_t start1, end1;
	clock_t start2, end2;
	clock_t start3, end3;
	clock_t start4, end4;
	clock_t start5, end5;
	clock_t start6, end6;
	clock_t start7, end7;
	clock_t start8, end8;
	clock_t start9, end9;
	clock_t start10, end10;
	clock_t start11, end11;
	clock_t start12, end12;
	clock_t start13, end13;
	clock_t start14, end14;
	clock_t start15, end15;
	clock_t start16, end16;
	clock_t start17, end17;
	clock_t start18, end18;
	clock_t start19, end19;
	clock_t start20, end20;
	double duration;

	int a[50001];
	int n = 50001;//數組元素的個數,即隨機數的個數
	
	Random(a, n, 1, 32767);
	start9 = clock();
	QuickSort_up(a, 0, n - 1);
	end9 = clock();
	duration = (double)(end9 - start9) / CLOCKS_PER_SEC;
	save(a, "快速排序(正向)", duration);
	cout << "快速排序(正向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start10 = clock();
	QuickSort_down(a, n);
	end10 = clock();
	duration = (double)(end10 - start10) / CLOCKS_PER_SEC;
	save(a, "快速排序(反向)", duration);
	cout << "快速排序(反向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start1 = clock();
	InsertSort_up(a, n);
	end1 = clock();
	duration = (double)(end1 - start1) / CLOCKS_PER_SEC;
	save(a, "直接插入排序(正向)", duration);
	cout << "直接插入排序(正向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start2 = clock();
	InsertSort_down(a, n);
	end2 = clock();
	duration = (double)(end2 - start2) / CLOCKS_PER_SEC;
	save(a, "直接插入排序(反向)", duration);
	cout << "直接插入排序(反向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start3 = clock();
	BinaryInsertionSort_up(a, 0, n - 1);
	end3 = clock();
	duration = (double)(end3 - start3) / CLOCKS_PER_SEC;
	save(a, "折半插入排序(正向)", duration);
	cout << "折半插入排序(正向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start4 = clock();
	BinaryInsertionSort_down(a, n);
	end4 = clock();
	save(a, "折半插入排序(反向)", duration);
	cout << "折半插入排序(反向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start5 = clock();
	TwoInsertSort_up(a, n);
	end5 = clock();
	duration = (double)(end5 - start5) / CLOCKS_PER_SEC;
	save(a, "2-路插入排序(正向)", duration);
	cout << "2-路插入排序(正向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start6 = clock();
	TwoInsertSort_down(a, n);
	end6 = clock();
	duration = (double)(end6 - start6) / CLOCKS_PER_SEC;
	save(a, "2-路插入排序(反向)", duration);
	cout << "2-路插入排序(反向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start7 = clock();
	ShellSort_up(a, n);
	end7 = clock();
	duration = (double)(end7 - start7) / CLOCKS_PER_SEC;
	save(a, "希爾排序(正向)", duration);
	cout << "希爾排序(正向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start8 = clock();
	ShellSort_down(a, n);
	end8 = clock();
	duration = (double)(end8 - start8) / CLOCKS_PER_SEC;
	save(a, "希爾排序(反向)", duration);
	cout << "希爾排序(反向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start11 = clock();
	SelectSort_up(a, n);
	end11 = clock();
	duration = (double)(end11 - start11) / CLOCKS_PER_SEC;
	save(a, "選擇排序(正向)", duration);
	cout << "選擇排序(正向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start12 = clock();
	SelectSort_down(a, n);
	end12 = clock();
	duration = (double)(end12 - start12) / CLOCKS_PER_SEC;
	save(a, "選擇排序(反向)", duration);
	cout << "選擇排序(反向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start13 = clock();
	TreeSelectSort_up(a, n);
	end13 = clock();
	duration = (double)(end13 - start13) / CLOCKS_PER_SEC;
	save(a, "錦標賽排序(正向)", duration);
	cout << "錦標賽排序(正向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start14 = clock();
	TreeSelectSort_down(a, n);
	end14 = clock();
	duration = (double)(end14 - start14) / CLOCKS_PER_SEC;
	save(a, "錦標賽排序(反向)", duration);
	cout << "錦標賽排序(反向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start15 = clock();
	HeapSort_up(a, n);
	end15 = clock();
	duration = (double)(end15 - start15) / CLOCKS_PER_SEC;
	save(a, "堆排序(正向)", duration);
	cout << "堆排序(正向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start16 = clock();
	HeapSort_down(a, n);
	end16 = clock();
	duration = (double)(end16 - start16) / CLOCKS_PER_SEC;
	save(a, "堆排序(反向)", duration);
	cout << "堆排序(反向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start17 = clock();
	MergeSort_up(a, 0, 50000);
	end17 = clock();
	duration = (double)(end17 - start17) / CLOCKS_PER_SEC;
	save(a, "歸并排序(正向)", duration);
	cout << "歸并排序(正向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start18 = clock();
	MergeSort_down(a, 50001);
	end18 = clock();
	duration = (double)(end18 - start18) / CLOCKS_PER_SEC;
	save(a, "歸并排序(反向)", duration);
	cout << "歸并排序(反向):" << duration << "seconds" << endl;

	Random(a, n, 1, 32767);
	start19 = clock();
	RadixSort_up(a, n);
	end19 = clock();
	duration = (double)(end19 - start19) / CLOCKS_PER_SEC;
	save(a, "基數排序(正向)", duration);
	cout << "基數排序(正向):" << duration << "seconds" << endl;


	Random(a, n, 1, 32767);
	start20 = clock();
	RadixSort_down(a, n);
	end20 = clock();
	duration = (double)(end20 - start20) / CLOCKS_PER_SEC;
	save(a, "基數排序(反向)", duration);
	cout << "基數排序(反向):" << duration << "seconds" << endl;

	return 0;
}

運行結果

總結

以上是生活随笔為你收集整理的排序算法的性能比较的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。