/*冒泡排序*/
#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;
}