任务:用选择排序对不同类型的数组进行排序
#include<iostream>
using namespace std;
//交换函数
template<class t>
void myswap(t& a, t& b)
{
t temp = a;
a = b;
b = temp;
}
//排序函数
template<class T>
void test(T &array,int len)
{
for (int i = 0; i < len; i++)
{
int max = i;
for (int j = i + 1; j < len; j++)
{
if (array[max] < array[j])
{
max = j;
}
}
if (max != i)
{
swap(array[max], array[i]);
}
}
}
//打印数组模板
template<class a>
void printarr(a &arr,int len)
{
for (int i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
}
int main()
{
int arr[] = { 2,4,7,5,8,6,10 };
int len = sizeof(arr) / sizeof(arr[0]);
test(arr, len);
printarr(arr, len);
system("pause");
return 0;
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有