0
点赞
收藏
分享

微信扫一扫

c++小程序/数组排序

数组排序

1、定义交换两个整型数的函数swap

2、定义sort函数给数组排序,调用swap函数


Code
#include<iostream.h>
template<class T>
void swap(T &x,T &y)
{T temp;
    temp=x;
    x=y;
    y=temp;
}
template<class p>
void sort(p *a,p b)
{
    for(int i=0;i<10;i++)
{
    for(int z=i;z<10;z++)
    {
        if(a[i]>a[z])
            swap(a[i],a[z]);
    }
}
cout<<"输出运算后的结果:"<<endl;
for(int j=0;j<10;j++)
cout<<a[j]<<" ";
}
void main()
{
int a[10];
cout<<"请输入10个数值:"<<endl;
for(int n=0;n<10;n++)
cin>>a[n];
sort(a,10);
}



举报

相关推荐

0 条评论