考点
题解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <bits/stdc++.h>
using namespace std;
const int LEN = 10001;
int bubble_sort(int arr[], int len) { int cnt = 0; for (int i = 0; i < len - 1; ++i) { for (int j = 0; j < len - 1 - i; ++j) { if (arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); ++cnt; } } } return cnt; }
int main() { int n, arr[LEN]; cin >> n; for (int i = 0; i < n; ++i) cin >> arr[i]; cout << bubble_sort(arr, n); return 0; }
|
思路
根据两两交换的题面,很明显就是冒泡排序,要你求内部元素一共交换了多少次