#include <iostream>
using namespace std;
//..............方法1:冒泡排序(自上而下扫描,“重者沉”).........................//
void BubbleSort(int *list,int n)
{
int i;
int j;
int x;
int flag;
for (i=0;i<n-1;i++)//扫描n-1趟
{
flag = 0;
for (j=0;j<n-i-1;j++)//第i趟扫描组内进行比较排序
{
if (list[j]>list[j+1])
{
x = list[j];
list[j] = list[j+1];
list[j+1] = x;
flag = 1;
}
}
if (!flag)//若该趟扫扫描没有交换,则结束算法
{
break;
}
}
}
//.....................方法2:冒泡排序(自下而上扫描,“轻者浮”).........................//
void BSort(int *list,int n)
{
int i;
int j;
int temp;
int flag;
for (i=0;i<n-1;i++)
{
flag = 0;
for (j=n-1;j>i;j--)
{
if (list[j-1]>list[j])
{
temp = list[j-1];
list[j-1] = list[j];
list[j] = temp;
flag = 1;
}
}
if (!flag)
{
break;
}
}
}
int main()
{
int arr[6] = {5,4,8,1,2,3};
int i;
cout << "output arrey before sorting:" << endl;
for (i=0;i<6;i++)
{
cout << arr[i] << ",";
}
cout << endl;
//BubbleSort(arr,6);
BSort(arr,6);
cout << "output arrey after sorting:" << endl;
for (i=0;i<6;i++)
{
cout << arr[i] << ",";
}
cout << endl;
return 0;
}