比较气泡排序,选择排序和插入排序

8 篇文章 0 订阅
8 篇文章 0 订阅

比较3种排序算法:气泡排序,选择排序和插入排序

比较3种流行的排序算法:气泡排序,选择排序和插入排序。为此,将生成随机整数并将其保存在数组中。然后对要排序的同一数组制作3个副本。对于每种排序,以毫秒为单位测量计时并打印比较。
针对3种情况进行测试:
1)整数数量= 1000
2)整数数量= 10,000
3)整数数量= 100,000

Sorting.cpp

#include<iostream>
#include<array>
#include <iostream>
#include <chrono>
#include<random>
#include"time.h"

template<typename T, int num>
void BubbleSort(std::array<T, num>& arr)
{
	for (int k = num - 1; k > 0; --k)
	{
		for (int i = 0; i < k; ++i)
		{
			if (arr[i] > arr[i + 1])
			{
				T temp = arr[i];
				arr[i] = arr[i + 1];
				arr[i + 1] = temp;
			}
		}
	}
}

template<typename T, int num>
void SelectionSort(std::array<T, num>& arr)
{
	for (int k = 0; k < num - 1; ++k)
	{
		int minIndex = k;
		for (int i = k + 1; i < num; ++i)
		{
			if (arr[i] < arr[minIndex])
				minIndex = i;
		}

		// swap min with k if necessary
		if (k != minIndex)
		{
			T temp = arr[minIndex];
			arr[minIndex] = arr[k];
			arr[k] = temp;
		}
	}
}

template<typename T, int num>
void InsertionSort(std::array<T, num>& arr)
{
	for (int k = 1; k < num; ++k)
	{
		T temp = arr[k];
		int i = k;

		while (i > 0 && arr[i - 1] >= temp)
		{
			arr[i] = arr[i - 1];
			--i;
		}
		arr[i] = temp;
	}
}
using namespace std;
int main()
{
	std::cout << "---------The test number is 1000--------" << std::endl;
	std::array<int, 1000> intArray1;
	for(int i = 0; i < 1000; ++i)
			intArray1[i] = rand();
	std::cout << "The first 10 integers before sorting are: ";
	for (int i = 0; i < 10; ++i)
		std::cout << intArray1[i] << ", ";
	std::array<int, 1000> intArray2 = intArray1;
	std::array<int, 1000> intArray3 = intArray1;
	std::cout << std::endl;
	auto start{ chrono::steady_clock::now() };
	InsertionSort<int, 1000>(intArray1);
	auto end = chrono::steady_clock::now();
	std::cout << "Insertsort::Elapsed time in ms:" << chrono::duration_cast<chrono::milliseconds>(end - start).count() << endl;

	start = chrono::steady_clock::now();
	SelectionSort<int, 1000>(intArray2);
	end = chrono::steady_clock::now();
	std::cout << "SelectionSort::Elapsed time in ms:" << chrono::duration_cast<chrono::milliseconds>(end - start).count() << endl;

	start = chrono::steady_clock::now();
	BubbleSort<int, 1000>(intArray3);
	end = chrono::steady_clock::now();
	std::cout << "BubbleSort::Elapsed time in ms:" << chrono::duration_cast<chrono::milliseconds>(end - start).count() << endl;

	std::cout << "The first 10 integers after sorting are: ";
	for (int i = 0; i < 10; ++i)
		std::cout << intArray1[i] << ", ";

	std::cout << std::endl;
	std::cout << "----------------Test End----------------" << std::endl;
	std::cout << std::endl << std::endl;


	std::cout << "---------The test number is 10000--------" << std::endl;
	std::array<int, 10000> intArray4;
	for (int i = 0; i < 10000; ++i)
		intArray4[i] = rand();
	std::cout << "The first 10 integers before sorting are: ";
	for (int i = 0; i < 10; ++i)
		std::cout << intArray4[i] << ", ";
	std::array<int, 10000> intArray5 = intArray4;
	std::array<int, 10000> intArray6 = intArray4;
	std::cout << std::endl;
	start= chrono::steady_clock::now();
	InsertionSort<int, 10000>(intArray4);
	end = chrono::steady_clock::now();
	std::cout << "Insertsort::Elapsed time in ms:" << chrono::duration_cast<chrono::milliseconds>(end - start).count() << endl;

	start = chrono::steady_clock::now();
	SelectionSort<int, 10000>(intArray5);
	end = chrono::steady_clock::now();
	std::cout << "SelectionSort::Elapsed time in ms:" << chrono::duration_cast<chrono::milliseconds>(end - start).count() << endl;

	start = chrono::steady_clock::now();
	BubbleSort<int, 10000>(intArray6);
	end = chrono::steady_clock::now();
	std::cout << "BubbleSort::Elapsed time in ms:" << chrono::duration_cast<chrono::milliseconds>(end - start).count() << endl;

	std::cout << "The first 10 integers after sorting are: ";
	for (int i = 0; i < 10; ++i)
		std::cout << intArray4[i] << ", ";

	std::cout << std::endl;
	std::cout << "----------------Test End----------------" << std::endl;

	std::cout << std::endl << std::endl;


	std::cout << "---------The test number is 100000--------" << std::endl;
	static std::array<int, 100000> intArray7;
	for (int i = 0; i < 100000; ++i)
		intArray7[i] = rand();

	std::cout << "The first 10 integers before sorting are: ";
	for (int i = 0; i < 10; ++i)
		std::cout << intArray7[i] << ", ";
	static std::array<int, 100000> intArray8 = intArray7;
	static std::array<int, 100000> intArray9 = intArray7;
	std::cout<<std::endl;
	start = chrono::steady_clock::now();
	InsertionSort<int, 100000>(intArray7);
	end = chrono::steady_clock::now();
	std::cout << "Insertsort::Elapsed time in ms:" << chrono::duration_cast<chrono::milliseconds>(end - start).count() << endl;

	start = chrono::steady_clock::now();
	SelectionSort<int, 100000>(intArray8);
	end = chrono::steady_clock::now();
	std::cout << "SelectionSort::Elapsed time in ms:" << chrono::duration_cast<chrono::milliseconds>(end - start).count() << endl;

	start = chrono::steady_clock::now();
	BubbleSort<int, 100000>(intArray9);
	end = chrono::steady_clock::now();
	std::cout << "BubbleSort::Elapsed time in ms:" << chrono::duration_cast<chrono::milliseconds>(end - start).count() << endl;

	std::cout << "The first 10 integers after sorting are: ";
	for (int i = 0; i < 10; ++i)
		std::cout << intArray7[i] << ", ";

	std::cout << std::endl;
	std::cout << "----------------Test End----------------" << std::endl;

	system("pause");
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值