01 选择排序的思想
02 选择排序的实现
03 例题讲解
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
int i, j, min_index;
for (i = 0; i < n-1; i++) {
min_index = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
if (min_index != i) {
swap(arr[min_index], arr[i]);
}
}
}
int main() {
int n;
cin >> n;
int treasures[n];
for(int i = 0; i < n; i++) {
cin >> treasures[i];
}
selectionSort(treasures, n);
for(int i = 0; i < n; i++) {
cout << treasures[i] << ' ';
}
cout << endl;
return 0;
}