函数作为函数参数对数组排序
代码如下:
#include<iostream>
using namespace std;
template <typename T>
bool shengxu(T a,T b)
{
return a>b;//升序排列
}
template <typename T>
bool jiangxu(T a,T b)
{
return a<b;//降序排列
}
template <typename T>
void my_sort(T *a,int n**,bool(*bf)(T,T)**)
{
int i,j;
T t;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(bf(a[i],a[j]))
{t=a[i];a[i]=a[j];a[j]=t;}
}
template <typename T>
void print(T a[],int n)
{
int i;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
int main()
{
int a[10] = {7,4,5,23,2,73,41,52,28,60};
print(a,10);
my_sort(a,10,shengxu);
print(a,10);
my_sort(a,10,jiangxu);
print(a,10);
return 0;
}