2. The most basic algorithm: bubble sort

  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 }

Result of running in VS2010:

image

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324602624&siteId=291194637