bubble_sort

冒泡排序法:

 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 }
原文地址:https://www.cnblogs.com/qinduanyinghua/p/5560226.html