SORTING NUMBERS

Muthumari_B · updated October 03, 2021 · C++
// SORTING Numbers in ARRAY 


#include<iostream>
using namespace std;
int main()
{
    int a[50],n,i,j,temp;
    cout<<"\n SORTING Numbers"<<endl;
    cout<<"\n Enter a Limit of Numbers : ";
    cin>>n;
    cout<<n<<endl;
    cout<<"\n Enter a Elements : ";
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<n;i++)
    {
        cout<<a[i]<<",";
    }
    cout<<endl;
    
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]>a[j])
            {
              temp=a[i];
              a[i]=a[j];
              a[j]=temp;
            }   
        }
    }
    cout<<"\n ASCENDING ORDER : "<<endl<<"\n\t";
    for(i=0;i<n;i++)
    {
        cout<<a[i]<<"  ";
    }     
    cout<<"\n\n DESCENDING ORDER :"<<endl<<"\n\t";
    for(i=n-1;i>=0;i--)
    {
        cout<<a[i]<<"  ";
    } 
    return 0;
}

Comments

Please sign up or log in to contribute to the discussion.