选择排序代码
#include <iostream>
using namespace std;
void SelectSort(int32_t numList[], int32_t len) {
for (auto i = 0; i < len; i++) {
auto min = i; //标记最新数的下标
for (auto j = i + 1; j < len; j++) {
if (numList[min] > numList[j]) {
//找到更小的数,记录下标
min = j;
}
}
auto tmp = numList[i];
numList[i] = numList[min];
numList[min] = tmp;
}
}
void main() {
int32_t numList[] = { 31, 41, 59, 26, 41, 58, 12 };
int32_t count = sizeof(numList) / sizeof(numList[0]);
SelectSort(numList, count);
for (auto num : numList) {
std::cout << num << " ";
}
std::cout << std::endl;
}
返回排序算法分析总结
本文介绍了一个简单的选择排序算法实现,并提供了完整的C++代码示例。该算法通过不断寻找未排序部分的最小元素并将其放置到已排序序列的末尾来完成排序过程。文章最后展示了排序后的结果。
1790

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



