1 #include <iostream>
2 using namespace std;
3
4 bool Greater2(int n1,int n2)
5 {
6 return n1 > n2;
7 }
8 bool Greater1(int n1,int n2)
9 {
10 return n1 < n2;
11 }
12 bool Greater3(double d1,double d2)
13 {
14 return d1 < d2;
15 }
16
17 template <class T1,class T2>
18 void mysort(
19 T1* p,T1* q,T2 op){
20 for(T1* i = p;i < q;++ i){
21 for(T1* j = i + 1;j < q;++ j)
22 if(!op(*i,*j))
23 swap(*i,*j);
24 }
25 }
26 #define NUM 5
27 int main()
28 {
29 int an[NUM] = { 8,123,11,10,4 };
30 mysort(an,an+NUM,Greater1); //从小到大排序
31 for( int i = 0;i < NUM; i ++ )
32 cout << an[i] << ",";
33 mysort(an,an+NUM,Greater2); //从大到小排序
34 cout << endl;
35 for( int i = 0;i < NUM; i ++ )
36 cout << an[i] << ",";
37 cout << endl;
38 double d[6] = { 1.4,1.8,3.2,1.2,3.1,2.1};
39 mysort(d+1,d+5,Greater3); //将数组从下标1到下标4从小到大排序
40 for( int i = 0;i < 6; i ++ )
41 cout << d[i] << ",";
42 return 0;
43 }