4个排序对比之结果及评价
  • 板块灌水区
  • 楼主lizesheng520
  • 当前回复5
  • 已保存回复5
  • 发布时间2024/10/13 10:19
  • 上次更新2024/10/13 12:11:05
查看原帖
4个排序对比之结果及评价
1207843
lizesheng520楼主2024/10/13 10:19

勇敢勇敢我的冒泡~

std::sort()bubbleinsertshell
time116.5822ms51228.3ms6731.83ms20.7231ms
time216.4295ms70814.4ms10497.3ms19.5011ms
time316.4111ms50867.1ms7738.26ms21.5137ms
time416.3882ms50432.3ms6713.18ms19.7919ms
time517.5525ms73575.7ms7763.7ms19.7787ms
平均16.6727ms59383.56ms7888.854ms20.2617ms
附代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <time.h>
#include <windows.h>
#include <assert.h>
using namespace std;

int main(){
	time_t t;
	srand(unsigned(t));
	int a[100000],b[100000];
	for(int i=0;i<100000;i++){
		b[i] = rand() % 2147483647;
	}
	for(int i=0;i<100000;i++){
		a[i]=b[i];
	}
	{
		double time;
		LARGE_INTEGER nFreq;
		LARGE_INTEGER nBeginTime;
		LARGE_INTEGER nEndTime;
		QueryPerformanceFrequency(&nFreq);
		QueryPerformanceCounter(&nBeginTime);
		sort(a,a+100000);
		QueryPerformanceCounter(&nEndTime);
		time = (double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;
		cout << "std::sort() Time: " << time*1000 << "ms" << endl;
	}
	for(int i=0;i<100000;i++){
		a[i]=b[i];
	}
	{
		double time;
		LARGE_INTEGER nFreq;
		LARGE_INTEGER nBeginTime;
		LARGE_INTEGER nEndTime;
		QueryPerformanceFrequency(&nFreq);
		QueryPerformanceCounter(&nBeginTime);
		for(int i=1;i<100000;i++)
		{
			int x=a[i],j=i-1;
			while(j>=0&&x<a[j]){
				a[j+1]=a[j];
				j--;
			}
			a[j+1]=x;
		}
		QueryPerformanceCounter(&nEndTime);
		time = (double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;
		cout << "\nInsert Time: " << time*1000 << "ms" << endl;
	}
	for(int i=0;i<100000;i++){
		a[i]=b[i];
	}
	{
		double time;
		LARGE_INTEGER nFreq;
		LARGE_INTEGER nBeginTime;
		LARGE_INTEGER nEndTime;
		QueryPerformanceFrequency(&nFreq);
		QueryPerformanceCounter(&nBeginTime);
		for(int i=0;i<100000-1;i++){
			for(int j=0;j<100000-1-i;j++){
				if(a[j]<a[j+1]){
					swap(a[j],a[j+1]);
				}
			}
		}
		QueryPerformanceCounter(&nEndTime);
		time = (double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;
		cout << "\nBubble Time: " << time*1000 << "ms" << endl;
	}
	for(int i=0;i<100000;i++){
		a[i]=b[i];
	}
	{
		double time;
		LARGE_INTEGER nFreq;
		LARGE_INTEGER nBeginTime;
		LARGE_INTEGER nEndTime;
		QueryPerformanceFrequency(&nFreq);
		QueryPerformanceCounter(&nBeginTime);
		assert(a);
		int gap = 100000;
		while (gap > 1){
			gap = gap / 3 + 1;
			for (int i = 0; i < 100000 - gap; i++){
				int end = i;
				int x = a[end + gap];
				while (end >= 0){
					if (a[end] > x){
						a[end + gap] = a[end];
						end -= gap;
					} else {
						break;
					}
				}
				a[end + gap] = x;
			}
		}
		QueryPerformanceCounter(&nEndTime);
		time = (double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;
		cout << "\nShell Time: " << time*1000 << "ms" << endl;
	}
	system("pause");
	return 0;
}

总结:std::sort()<shell<insert<bubble (时间) 综上,std::sort() ------ NB!!

2024/10/13 10:19
加载中...