include<iostream>
using namespace std;
template<class T>
//交换元素
void sp(T &a, T &b){
T temp = a;
a = b;
b = temp;
}
//选择算法
template<class T>
void test(T arr[],int len){
for (int i = 0; i < len; i++){
int max = i;
for (int j = i + 1; j < len; j++){
if (arr[max] < arr[j])
max = j;
}
//交换max和i元素
if (max != i) sp(arr[max], arr[i]);
}
}
int main(){
char arr[] = "45663772";
int len = sizeof(arr) / sizeof(char);
test(arr,len);
//从大到小排序
for (int i = 0; i < len; i++)
cout << arr[i] << " ";
}
c++实现从大到小排序 模板
最新推荐文章于 2024-10-04 22:43:57 发布