选择排序代码
#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;
}
返回排序算法分析总结