假设产生了13个随机数,在C语言中,如何让他们按照大小排列呢?

来源:百度知道 编辑:UC知道 时间:2024/05/14 12:25:08
继续。

//假设产生了13个随机数,在C语言中,如何让他们按照大小排列呢?
#include<iostream>
using namespace std;
int const M=13;
int main()
{
int a[M];
int temp;
cout<<"请输入13个任意整数:";
for(int k=1;k<14;k++)
{cout<<"NO "<<k<<" ";
cin>>a[M];
}
for(int j=0;j<12;j++)
for(int i=0;i<12-j;i++)
if(a[i]>=a[i+1])
{temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;}
for(int l=0;l<13;l++)
cout<<a[l]<<endl;
return 0;
}

简单的一个冒泡法就OK了