template<typename T> void Swap(T& a, T& b)
{
T c = a;
a = b;
b = c;
}
template<typename T>
void SelectionSort(T* arr, int n) //选择排序
{
for(int i = 0; i <= n - 2; i++)
{
int min = i;
for (int j = i + 1; j <= n - 1; j++)
{
if (arr[j] < arr[min])
{
min = j;
}
}
Swap<T>(arr[i], arr[min]);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
string a[] = { "asdas", "tdg", "bfds", "dwdad" };
SelectionSort(a, 4);
for(int i = 0; i < 4; i++)
{
cout<< a[i] << endl;
}
return 0;
}
C++选择排序模板
最新推荐文章于 2017-03-19 17:49:00 发布