起泡排序(bubble sort)

起泡排序
        起泡排序比较相邻元素,若为逆序,则交换元素,这种排序一般需要多次遍历数据。在第一次便利中,比较数组前两项,若为逆序,则进行交换;则比较下一对元素,即数组位置2和位置3,若为逆序,则进行交换,继续此过程,每次比较和交换两个元素,直到数组结束。

#include<iostream>
using namespace std;
void sort(int arr[],int size);
int main()
{
	const int SIZE=10;
	int aarray[SIZE]={4,8,9,6,3,2,15,7,1,12};
	for(int i=0;i<SIZE;i++)
		cout<<"the "<<i+1<<"th item is"<<aarray[i]<<endl;
	sort(aarray,SIZE);
	cout<<"the sorted array aarray is :"<<endl;
	for(int i=0;i<SIZE;i++)
		cout<<"the "<<i+1<<"th item is "<<aarray[i]<<endl;
	return 0;
}
void sort(int arr[],int size)
{
	bool sorted=false;
	for(int i=1;(i<size)&&(!sorted);i++)
	{
		sorted=true;
		int swapmax;
		for(int j=0;j<size-i;j++)
		{
			if(arr[j]>arr[j+1])
			{
				swapmax=arr[j];
				arr[j]=arr[j+1];
				arr[j+1]=swapmax;
				sorted=false;
			}
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值