找最小的和第一个位置做交换,递归下去
#include <iostream>
using namespace std;
void selectionSort(int arr[],int n) {
for (int i = 0; i < n; ++i) {
//寻找【i,n】区间的最小值
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[minIndex]) minIndex = j;
swap(arr[i], arr[minIndex]);
}
}
}
int main() {
int a[10] = {10,9,8,7,6,5,4,3,2,1};
selectionSort(a,10);
for (int i = 0; i < 10; ++i) {
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}
![img_4c23e54602f8b6778d28b97d61676854.png](https://i-blog.csdnimg.cn/blog_migrate/30c1617fbb3a183768a29f4058b22a02.png)
image.png