bubble_sort

时间:2023-03-09 07:53:24
bubble_sort

冒泡排序法:

 #include <iostream>
#include <algorithm>
using namespace std; int main(){
int n;
cin >> n;
vector<int> a(n);
for(int a_i = ;a_i < n;a_i++){
cin >> a[a_i];
}
int numberOfSwaps = ;
for (int i = ; i < n ; i++) //冒泡排序
for (int j = ; j < n - - i ; j++) {
if (a[j] > a[j + ]) {
swap(a[j], a[j + ]);
numberOfSwaps++;
}
}
cout << "Array is sorted in " << numberOfSwaps << " swaps." << endl;
cout << "First Element: " << a[] << endl;
cout << "Last Element: " << a[n-] << endl;
return ;
}