码迷,mamicode.com
首页 > 其他好文 > 详细

bubble_sort

时间:2016-06-05 11:08:37      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

冒泡排序法:

 

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 int main(){
 6     int n;
 7     cin >> n;
 8     vector<int> a(n);
 9     for(int a_i = 0;a_i < n;a_i++){
10        cin >> a[a_i];
11     }
12     int numberOfSwaps = 0;
13     for (int i = 0; i < n ; i++) //冒泡排序
14         for (int j = 0; j < n - 1 - i ; j++) {
15             if (a[j] > a[j + 1]) {
16                 swap(a[j], a[j + 1]);
17                 numberOfSwaps++;
18             }
19     }
20     cout << "Array is sorted in " << numberOfSwaps << " swaps." << endl;
21     cout << "First Element: " << a[0] << endl;
22     cout << "Last Element: " << a[n-1] << endl;
23     return 0;
24 }

 

bubble_sort

标签:

原文地址:http://www.cnblogs.com/qinduanyinghua/p/5560226.html

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