简单冒泡排序

4 篇文章 0 订阅
#include <iostream>
#include <string>
using namespace std;

void paixv2(int r[], int a);
void paixv(int r[], int a){
	int i, j, t;
	for(i=0; i<a-1; i++){
		for(j=0; j<a-i-1; j++){
			if(r[j]>r[j+1]){
				t = r[j];
				r[j] = r[j+1];
				r[j+1] = t;
			}
		}
	}
	for(i=0; i<5; i++){
		cout<<r[i];
	}

};
void bubblesort(int a[], int n){
	int i, j, t;
	for (i=0; i<n-1;i++)
		for(j=0; j<n-i-1; j++)
			if(a[j]>a[j+1]){   //交换两个变量的值
				t = a[j];
				a[j] = a[j+1];
				a[j+1] = t;
				
			}
			cout<<"排序后为: "<<endl;
			for (i=0;i<5;i++){
				cout<<a[i]<<"   ";
			}
};
void bubblesort2(int r[],int n)
{
	int i; 
	int bound,exchange=n-1;  //bound作为边界  exchange是交换的位置
	while(exchange!=0)
	{
		bound=exchange;
		exchange=0;
		for(int j=0;j<bound;j++)
			if(r[j]>r[j+1])
			{
			  int t=r[j];r[j]=r[j+1];r[j+1]=t;
				exchange=j;
			}
	}
	for (i=0;i<5;i++){
				cout<<r[i]<<"   ";
			}
}
void main(){
	int a[5];
	cout<<"请输入五个数: "<<endl;
	cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4];
	
	

	int n = 5;
	paixv(a,n);
	cout<<endl;
	paixv2(a, n);
	bubblesort(a, n);
	bubblesort2(a, n);
	int b;
	cin>>b;

}
void paixv2(int r[], int a){
	int i, j, t, bound, exchange;
	exchange = a-1;
	 
	while(exchange!=0){

		bound = exchange;
		exchange = 0;

		for(j=0; j<bound; j++){
		
			if(r[j]>r[j+1]){
				t = r[j];
				r[j] = r[j+1];
				r[j+1] = t;
				exchange = j;
			}
		}
	}
	for(i=0; i<5; i++){
		cout<<r[i];
	}
	
	
}

	



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值