/* SelectSort.hpp */ #ifndef _SELECT_SORT_H_ #define _SELECT_SORT_H_ template<typename T> bool SelectSort(T *pInput, int nLen) { int i = 0; int j = 0; int nMin = 0; T tTemp; if(!pInput) { return false; } for(i=0; i<nLen; i++) { nMin = i; for(j=i+1; j<nLen; j++) { if(pInput[j]<pInput[nMin]) { nMin = j; } } if(nMin != i) { tTemp = pInput[i]; pInput[i] = pInput[nMin]; pInput[nMin] = tTemp; } } return true; } #endif /* main.cpp */ #include <iostream> #include "SelectSort.hpp" using namespace std; int main() { int i = 0; int a[10] = {1,4,7,2,5,8,3,6,9,0}; if(SelectSort<int>(a,10)==false) { cout<<"排序失败"<<endl; } else { for(i=0; i<10; i++) { cout<<a[i]<<'\t'; } cout<<endl; } return 0; }