一、思路
每次取剩下没排序的数中的最小数,然后,填到对应位置。(可以使用a[0]位置作为暂存单元)
如下:
二、实现程序:
#include <iostream>
using namespace std;
const int maxSize = 100;
template<class T>
void SelectSort(T arr[], int n); // 选择排序
int main(int argc, const char * argv[]) {
int i, n, arr[maxSize];
cout << "请输入要排序的数的个数:";
cin >> n;
cout << "请输入要排序的数:";
for(i = 1; i <= n; i++) // arr[0]不存放值,用来做暂存单元
cin >> arr[i];
cout << "排序前:" << endl;