算法源码:
//BubbleSort.cpp
#include <iostream>
using namespace std;
void BubbleSort(int a[], int n)
{
for(int i=n-1;i>0;i--)
{
for(int j=0;j<i;j++)
{
if (a[j]>a[j+1])
{
int tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}
}
int main()
{
int a[]={4,3,6,9,7,5,1,10,2,15,8,7,6};
BubbleSort(a,sizeof(a)/sizeof(a[0]));
cout<<"after bubble sort."<<endl;
for (int i=0;i<sizeof(a)/sizeof(a[0]);i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
system("pause");
return 0;
}