冒泡排序【代码】

思路请参考:

http://www.cnblogs.com/kkun/archive/2011/11/23/2260280.html 

代码如下:

 1 // 171027冒泡排序.cpp: 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include <iostream>
 6 
 7 using namespace std;
 8 
 9 void bubble_sort(int a[],int len)//按升序排列
10 {
11     int swap = 0;
12     for (int i = 0; i < len-1; i++)//多少趟
13     {
14         for (int j = 0; j < len-1-i; j++)//每趟比较多少次
15         {
16             if (a[j] > a[j+1])//如果想按降序排列,这里改为a[j] < a[j+1]
17             {
18                 swap = a[j];
19                 a[j] = a[j+1];
20                 a[j+1] = swap;
21             }
22         }
23     }
24     cout << "排序之后的数组序列: ";
25     for (int i = 0; i < len; i++)
26     {
27         cout << a[i] << ends;
28     }
29     cout << endl;
30 }
31 
32 int main()
33 {
34     int test[9] = { 1,4,6,9,7,2,8,5,3 };
35     cout << "排序之前的数组序列: ";
36     for (auto c : test)
37         cout << c << ends;
38     cout << endl;
39     bubble_sort(test,9);
40     return 0;
41 }
只有0和1的世界是简单的
原文地址:https://www.cnblogs.com/nullxjx/p/7745326.html