#include <iostream>
using namespace std;
/**
* 选择排序
* @tparam T 数组类型
* @param arr 数组名
* @param n 长度
*/
template<class T>
void selectionSort(T arr[], int n) {
for (int i = 0; i < n; ++i) {
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
swap(arr[minIndex], arr[i]);
}
}
int main() {
int arr[] = {-1, 0, -2, 1, 9, 0, 2, 5, 4, 3};
int len = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, len);
for (const int &temp: arr) {
cout << temp << " ";
}
cout << endl;
return 0;
}
选择排序-C++
最新推荐文章于 2022-03-19 22:58:13 发布