您的位置:首页 > 其它

(练习)冒泡排序

2014-07-21 19:35 204 查看
/*冒泡排序*/
#include <iostream>
using namespace std;
#define SIZE 10
void BubbleSort(int value[], int n)
{
	int temp;
	int flag = 1;//改进:用于判断当没有可交换的数据对时,直接结束排序
	int count = 0;
	for(int i = n-1; i > 0; i--)
	{
		for(int j = 0; j < n-1; j++)
		{
			if(value[j] > value[j+1])
			{
				temp = value[j];
				value[j] = value[j+1];
				value[j+1] = temp;
				flag = 0;
			}

		}
	
		if(flag == 1)
			break;
		count++;
		flag = 1;
	}
	for(i = 0; i < n; i++)
	{
		cout<<value[i]<<" ";
		cout<<endl;

	}
	cout<<"遍历数据 "<<count<<" 次"<<endl;
}
int main()
{
	int value[SIZE]={1,2,3,5,11,22,43,122,22,12};
	BubbleSort(value, SIZE);
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: