/*Copyright (c) 2016,烟台大学计算机学院
*All rights reserved.
*文件名称 :
*作 者 : 李钊
*完成日期 : 2016年5月31号
*版 本 号 : v1.0
*
*问题描述 : 排序函数模板
*输入描述 :
*程序输出 : 从小到大排序后的数组
*/
#include <iostream>
using namespace std;
template <class T>
void paixu(T a[],int n)
{
int i,j;
T temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
int main()
{
int s1[5]={5,4,3,2,1};
double s2[5]={5.5,4.4,3.3,2.2,1.1};
int i;
for(i=0;i<5;i++)
{
cout<<s1[i]<<" ";
}
cout<<endl;
for(i=0;i<5;i++)
{
cout<<s2[i]<<" ";
}
cout<<endl;
cout<<"排序后:"<<endl;
paixu(s1,5);
paixu(s2,5);
for(i=0;i<5;i++)
{
cout<<s1[i]<<" ";
}
cout<<endl;
for(i=0;i<5;i++)
{
cout<<s2[i]<<" ";
}
cout<<endl;
return 0;
}
运行结果:
学习心得:
函数模板的运用