选择排序的核心思想:
选择排序是一种简单直观的排序算法,下面是它的基本原理:
1. 不断选择剩余元素中的最小值
2. 将其放在数组已排序部分的末尾
3. 通过交换操作将最小元素归位
下面是我做的选择排序基本程序:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
void selectionSort(LL arry[], LL n){
for (LL i = 0; i < n - 1; i++) {
LL minIndex = i;
for (LL j = i + 1; j < n; j++) {
if (arry[j] < arry[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
swap(arry[i], arry[minIndex]);
}
}
}
int main() {
LL arry[] = {64, 25, 12, 22, 11};
LL n = sizeof(arry) / sizeof(arry[0]);
cout << "原始数组:";
for (LL i = 0; i < n; i++) {
cout << arry[i] << ' ';
}
selectionSort(arry, n);
cout << "\n排序后数组";
for (LL i = 0; i < n; i++) {
cout << arry[i] << ' ';
}
return 0;
}

被折叠的 条评论
为什么被折叠?



