冒泡排序再学习改进冒泡排序(c++)

 

未经博主同意不得私自转载!不准各种形式的粘贴复制本文及盗图!

 

经典的排序冒泡排序:


编译器:dev-c++ 5.11

c++版冒泡排序:

#include<iostream>
	using namespace std;
int main()
{
	int a[10]={7,3,4,5,654,77,3,5,199,688};
	for(int i=0;i<10;i++)
		for(int j=0;j<10-i-1;j++)
	{
		if(a[j]>a[j+1])
		{
			int swap=a[j];
			a[j]=a[j+1];
			a[j+1]=swap;
		}
	}
	for(int i=0;i<10;i++)
		cout<<a[i]<<'\t';
	return 0;
}


运行数据及结果:

 

Compilation results...
--------
- Errors: 0
- Warnings: 0
- Output Filename: F:\dev 程序\代码\sort.exe
- Output Size: 1.83190059661865 MiB
- Compilation Time: 1.00s
运行结果: 
3       3       4       5       5       7       77      199     654     688
--------------------------------
Process exited after 0.3036 seconds with return value 0


第一种改进方式:

 

加入一标志性变量position,用于标志某一趟排序过程中是否有数据交换,如果进行某一趟排序时并没有进行数据交换,则说明数据已经按要求排列好,可立即结束排序,避免不必要的比较过程

纯净版代码:

#include<iostream>
	using namespace std;
int main()
{
	int a[10]={7,3,4,5,654,77,3,5,199,688};
	int i=10;
	while(i>0)
	{
		int k=0;
		int position=0;
		for(int j=0;j<i;j++)

			if(a[j]>a[j+1])
		{
			k++;
			position=j;
			int swap=a[j];
			a[j]=a[j+1];
			a[j+1]=swap;
		}
		i=position;
	}
	for(i=0;i<10;i++)
		cout<<a[i]<<'\t'<<endl;
	return 0;
}


运行数据及结果:

 

Compilation results...
--------
- Errors: 0
- Warnings: 0
- Output Filename: F:\dev 程序\代码\sort.exe
- Output Size: 1.83208656311035 MiB
- Compilation Time: 0.92s
运行结果:
3
3
4
5
5
7
26
77
199
654

--------------------------------
Process exited after 0.303 seconds with return value 0


用于分析版-代码:

 

#include<iostream>
	using namespace std;
int main()
{
	int a[10]={7,3,4,5,654,77,3,5,199,688};
	int i=10;
	while(i>0)
	{
		int k=0;
		int position=0;
		for(int j=0;j<i;j++)

			if(a[j]>a[j+1])
		{
			k++;
			cout<<"j的值:"<<j<<"第"<<k<<"次"<<endl;
			cout<<"比较的值:"<<a[j]<<'\t'<<a[j+1];
			position=j;


			int swap=a[j];
			a[j]=a[j+1];
			a[j+1]=swap;
			cout<<'\t'<<"交换后序列:";
			for(i=0;i<10;i++)
				cout<<a[i]<<'\t';
			cout<<endl;	cout<<endl;
			//cout<<a[j]<<'\t'<<a[j+1]<<'\t'<<endl;
		}
		i=position;
	}
	for(i=0;i<10;i++)
		cout<<a[i]<<'\t'<<endl;
	return 0;
}


运行数据及结果:

 

Compilation results...
--------
- Errors: 0
- Warnings: 0
- Output Filename: F:\dev 程序\代码\sort.exe
- Output Size: 1.83309745788574 MiB
- Compilation Time: 1.33s
运行结果:
j的值:0第1次
比较的值:7     3   交换后序列:3   7   4    5   654  77    3    5    199     688

j的值:1第2次
比较的值:7   4  交换后序列:3   4    7    5    654   77   3    5    199   688

j的值:2第3次
比较的值:7     5       交换后序列:3   4       5       7       654     77      3       5       199     688

j的值:4第4次
比较的值:654   77      交换后序列:3   4       5       7       77      654     3       5       199     688

j的值:5第5次
比较的值:654   3       交换后序列:3   4       5       7       77      3       654     5       199     688

j的值:6第6次
比较的值:654   5       交换后序列:3   4       5       7       77      3       5       654     199     688

j的值:7第7次
比较的值:654   199     交换后序列:3   4       5       7       77      3       5       199     654     688

j的值:4第1次
比较的值:77    3       交换后序列:3   4       5       7       3       77      5       199     654     688

j的值:5第2次
比较的值:77    5       交换后序列:3   4       5       7       3       5       77      199     654     688

j的值:3第1次
比较的值:7     3       交换后序列:3   4       5       3       7       5       77      199     654     688

j的值:4第2次
比较的值:7     5       交换后序列:3   4       5       3       5       7       77      199     654     688

j的值:2第1次
比较的值:5     3       交换后序列:3   4       3       5       5       7       77      199     654     688

j的值:1第1次
比较的值:4     3       交换后序列:3   3       4       5       5       7       77      199     654     688

3
3
4
5
5
7
77
199
654
688

--------------------------------
Process exited after 0.5033 seconds with return value 0	


第二种改进方式:

 

 

这种改进是进行正向反向两边冒泡的方法实现排序

Ps:注释掉一边也能完成排序

纯净版-完整版-代码:

#include<iostream>
	using namespace std;
int main()
{
	int a[10]={7,3,22,43,654,77,16,5,199,688};
	int high=10-1;
	int low=0;
	while(low<high)
	{

		for(int j=low;j<high;++j)

			if(a[j]>a[j+1])
		{

			int swap=a[j];
			a[j]=a[j+1];
			a[j+1]=swap;
		}
		--high;
		for(int j=high;j>low;--j)

			if(a[j]<a[j-1])
		{

			int swap=a[j];
			a[j]=a[j-1];
			a[j-1]=swap;
		}
		++low;
	}
	for(int i=0;i<10;i++)
		cout<<a[i]<<'\t'<<endl;
	return 0;
}


运行数据及结果:

 

Compilation results...
--------
- Errors: 0
- Warnings: 0
- Output Filename: F:\dev 程序\代码\sort.exe
- Output Size: 1.83257484436035 MiB
- Compilation Time: 0.92s
运行结果:
3
5
7
16
22
43
77
199
654
688

--------------------------------
Process exited after 0.3265 seconds with return value 0


用于分析-注释掉一边冒泡排序-代码:

 

#include<iostream>
	using namespace std;
int main()
{
	int a[10]={7,3,22,43,654,77,16,5,199,688};
	int high=10-1;
	int low=0;
	while(low<high)
	{

		for(int j=low;j<high;++j)

			if(a[j]>a[j+1])
		{

			int swap=a[j];
			a[j]=a[j+1];
			a[j+1]=swap;
		}
		--high;
		//		for(int j=high;j>low;--j)
		//
		//			if(a[j]<a[j-1])
		//		{
		//		
		//			int swap=a[j];
		//			a[j]=a[j-1];
		//			a[j-1]=swap;
		//		}
		//		++low;
	}
	for(int i=0;i<10;i++)
		cout<<a[i]<<'\t'<<endl;
	return 0;
}

运行数据及结果:

 

Compilation results...
--------
- Errors: 0
- Warnings: 0
- Output Filename: F:\dev 程序\代码\sort.exe
- Output Size: 1.83208656311035 MiB
- Compilation Time: 0.89s
运行结果:
 3
5
7
16
22
43
77
199
654
688

--------------------------------
Process exited after 0.333 seconds with return value 0


ps:一切内容均是本人根据网上各种途径,翻阅书籍等方式总结提炼的,如果设计版权希望能及时提醒更改。同时希望注重保护他人成果!


 






 

转载于:https://www.cnblogs.com/zhuhengjie/p/5966864.html

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值