使用c/c++实现冒泡排序算法和比较排序算法的效率

本文对比了C++中冒泡排序与普通比较排序的效率,详细解析了两种算法的实现过程,展示了不同数据序列下各自的运行效果。冒泡排序在已排序序列上表现更优,而普通比较排序则无论序列状态如何,执行次数固定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用c/c++实现冒泡排序算法和比较排序算法的效率

实现目的

通过对比冒泡法排序和普通的比较排序,阐述冒泡法的排序效率,从而达到更好的掌握冒泡法的目的

冒泡法排序和比较排序的实现方法

普通比较排序算法

#include <iostream>
#include <vector>

using namespace std;

/**
 * @brief    normal_sort
 *
 *     normal compare algorithm to sort data sequence form smallest ---> biggest
 *
 * @param    vec
 */
void normal_sort(vector<int> &vec)
{
	int i, j;
	int size;
	int temp;

	size = vec.size();

	for(auto v : vec)
	{
		cout << v << " ";
	}
	cout << endl;
	cout << endl;

	for(i=0; i<size-1; ++i)
	{
		for(j=i+1; j<size; ++j)
		{
			if(vec.at(i) > vec.at(j) )
			{
				temp = vec.at(i);
				vec.at(i) = vec.at(j);
				vec.at(j) = temp;
			}
		}

		for(auto v : vec)
		{
			cout << v << " ";
		}
		cout << endl;
	}
}

int main(int argc, char **argv)
{
	vector<int> intvec;
	vector<int> intvec1;

	vector<int>().swap(intvec);
	vector<int>().swap(intvec1);
	intvec.push_back(4);
	intvec.push_back(5);
	intvec.push_back(7);
	intvec.push_back(1);
	intvec.push_back(8);
	intvec.push_back(3);

	intvec1.push_back(1);
	intvec1.push_back(2);
	intvec1.push_back(3);
	intvec1.push_back(4);
	intvec1.push_back(5);
	intvec1.push_back(6);

	cout << "num: the bigger is down to bottom, and the smaller is up to the top" << endl;
	normal_sort(intvec);
	cout << "num1: if sequence is alredy sort, just run once time" << endl;
	normal_sort(intvec1);

	return 0;
}

执行结果

tony@Tech:~/linux/fight$ ./normal_sort 
num: the bigger is down to bottom, and the smaller is up to the top
4 5 7 1 8 3 

1 5 7 4 8 3 
1 3 7 5 8 4 
1 3 4 7 8 5 
1 3 4 5 8 7 
1 3 4 5 7 8 
num1: if sequence is alredy sort, just run once time
1 2 3 4 5 6 

1 2 3 4 5 6 
1 2 3 4 5 6 
1 2 3 4 5 6 
1 2 3 4 5 6 
1 2 3 4 5 6 

冒泡排序算法

#include <iostream>
#include <vector>

using namespace std;

/**
 * @brief    bubble_sort
 *     
 *     bubble algorithm to sort data sequence form smallest ---> biggest
 *
 * @param    vec
 */
void bubble_sort(vector<int> &vec)
{
	bool flag;
	int i, j;
	int size;
	int temp;

	size = vec.size();

	for(auto v : vec)
	{
		cout << v << " ";
	}
	cout << endl;
	cout << endl;

	for(i=size-1, flag=true; i>1 && flag; --i)
	{
		flag = false;

		for(j=0; j<i; ++j)
		{
			if(vec.at(j) > vec.at(j+1) )
			{
				temp = vec.at(j);
				vec.at(j) = vec.at(j+1);
				vec.at(j+1) = temp;
				flag = true;
			}
		}

		for(auto v : vec)
		{
			cout << v << " ";
		}
		cout << endl;
	}
}

int main(int argc, char **argv)
{
	vector<int> intvec;
	vector<int> intvec1;

	vector<int>().swap(intvec);
	vector<int>().swap(intvec1);
	intvec.push_back(4);
	intvec.push_back(5);
	intvec.push_back(7);
	intvec.push_back(1);
	intvec.push_back(8);
	intvec.push_back(3);

	intvec1.push_back(1);
	intvec1.push_back(2);
	intvec1.push_back(3);
	intvec1.push_back(4);
	intvec1.push_back(5);
	intvec1.push_back(6);

	cout << "num: the bigger is down to bottom, and the smaller is up to the top" << endl;
	bubble_sort(intvec);
	cout << "num1: if sequence is alredy sort, just run once time" << endl;
	bubble_sort(intvec1);

	return 0;
}

执行结果

tony@Tech:~/linux/fight$ ./bubble_sort 
num: the bigger is down to bottom, and the smaller is up to the top
4 5 7 1 8 3 

4 5 1 7 3 8 
4 1 5 3 7 8 
1 4 3 5 7 8 
1 3 4 5 7 8 
num1: if sequence is alredy sort, just run once time
1 2 3 4 5 6 

1 2 3 4 5 6 
tony@Tech:~/linux/fight$ gedit normal_sort.cpp 
tony@Tech:~/linux/fight$ 

结论

通过比较两种算法的差异得知:

  • 冒泡排序算法:先把最大的值沉到最低下,最小的值一步一步以冒泡的方式向前移动,而如果已经排序好的序列,只需要执行一次,效率是最高的。

  • 比较排序:不管原来是否排好序,都是按照固定的次数排序,执行次数是固定的
    c o u n t e r = n ∗ ( n − 1 ) 2 counter = \frac{n*(n-1)}{2} counter=2n(n1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fttony2020

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值