排列函数模板

来源:互联网 发布:matlab调用c和java 编辑:程序博客网 时间:2024/06/11 10:54


#include<iostream>
using namespace std;
template <class T>
void Sort(T  str[] ,int n)
{
    T t;
    int i,j;
    for(i=0;i<n;i++)
        for(j=0;j<n-i-1;j++)
        if(str[j]>str[j+1])
    {
        t=str[j];str[j]=str[j+1];str[j+1]=t;
    }

}
int main()
{
    int i;
    int a[]= {4,5,2,8,9,3};
    double b[]= {3.5, 6.7, 2, 5.2, 9.2, 10.3};
    Sort(a,6);
    Sort(b,6);
    for(i=0; i<6; i++)
        cout<<a[i]<<"  ";
    cout<<endl;
    for(i=0; i<6; i++)
        cout<<b[i]<<"  ";
    cout<<endl;
    return 0;
}

0 0