2、最基本的算法:冒泡排序

  1 #include<iostream>
  2 
  3 using namespace std;
  4 
  5 void BubbleSort(int list[],int n);
  6 int main()
  7 {
  8 	int a[]={2,4,6,8,0,5,1,3,7,9};
  9 	BubbleSort(a,10);
 10 
 11 	for(int k=0;k<10;k++)
 12 		cout<<a[k]<<"";
 13 	cout<<endl;
 14 	system("pause");
 15 	return 0;
 16 }
 17 void BubbleSort(int list[],int n)
 18 {
 19 	for(int i=0;i<n-1;i++)
 20 	{
 21 		for(int j=0;j<n-1-i;j++)
 22 		{
 23 			if(list[j]>list[j+1])
 24 				std::swap(list[j],list[j+1]);
 25 		}
 26 	}
 27 }

VS2010中运行结果:

image

原文地址:https://www.cnblogs.com/luanxin/p/8874522.html