补全9月11日至13日课评:为13名学生创建请假文档,为5个班级创建总结文档
This commit is contained in:
33
C++代码/选择排序.cpp
Normal file
33
C++代码/选择排序.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <bits/stdc++.h>
|
||||
#define endl '\n'
|
||||
using namespace std;
|
||||
|
||||
int n;
|
||||
int arr[100100];
|
||||
|
||||
int main(){
|
||||
cin >> n;
|
||||
for(int i = 1; i <= n; i++){
|
||||
cin >> arr[i];
|
||||
}
|
||||
|
||||
for(int i = 1; i < n; i++){ // i就是当前这个位置 找到一个最值,然后进行交换
|
||||
int maxx = arr[i]; // 当前的最大值
|
||||
int idx = i;
|
||||
for(int j = i + 1; j <= n; j++){
|
||||
// 打擂台
|
||||
if(arr[j] > maxx){
|
||||
maxx = arr[j];
|
||||
idx = j; // 记住最大值下标
|
||||
}
|
||||
}
|
||||
|
||||
swap(arr[idx], arr[i]);
|
||||
}
|
||||
|
||||
for(int i = 1; i <= n; i++){
|
||||
cout << arr[i] << " ";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user