码迷,mamicode.com
首页 > 编程语言 > 详细

冒泡排序【经典排序算法】【代码】

时间:2017-10-27 22:02:55      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:控制   排序算法   end   应用   blog   控制台   控制台应用程序   return   ret   

思路请参见

 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 }

 

冒泡排序【经典排序算法】【代码】

标签:控制   排序算法   end   应用   blog   控制台   控制台应用程序   return   ret   

原文地址:http://www.cnblogs.com/journal-of-xjx/p/7745326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!