C++ Program to sort a list (descending order)

//sort_desc
#include <iostream>
using namespace std;
int main()
{  
    int n;        //size of list
    int i,j;         //for loops for reading, swapping etc
    double temp;    //swapping
    cout<<"Enter the size of the list"<<endl;
    cin>>n;
    cout<<"Enter the elements of the list\n";
    double a[n];    //the list of no.s to be sorted
    for (i=0;i<n;i++)    //reading the elements
    {
    cin>>a[i];
    }
    for (i=0;i<n;i++)    //sorting
    {
        for (j=i;j<n;j++)
        {
            if (a[i]<a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    cout<<"\nThe sorted list is \n";
        for (i=0;i<n;i++)
    {
    cout<<a[i]<<endl;
    }
    return 0;
}
  
[wpedon id="7041" align="center"]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.