C program to sort Array in descending order

This program is made in the C programming language.  Below is the code for the C program to sort the array in the descending order –

/* C program to arrange or sort the array in the Descending order */
#include<stdio.h>
#include<conio.h>
void main()
{
     int ar[100],j,n,i,tmp;
     
     printf(" Enter the size of the array \t");
     scanf("%d",&n);
     
     printf("Now enter the elements in the array \t");
     for(i=0;i<n;i++)
     {
           scanf("%d",&ar[i]);
     }
     
     printf("\n Array is - ");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }
     
     for(i=0;i<n;i++)
     {
           for(j=0;j<n-i;j++)
           {
                 if(ar[j]<ar[j+1])
                 {
                       tmp=ar[j+1];
                       ar[j+1]=ar[j];
                       ar[j]=tmp;
                 }
           }
     }
     
     printf("\n\n Array in the Descending order is - \n");
     for(i=1;i<=n;i++)
     {
           printf("\t %d",ar[i]);
     }
     getch();
}

Logic – This program uses the bubble sort technique to sort all the elements of the array in the descending order. Bubble sort technique uses the system of passes to sort the elements in the desired order. The number of passes is one less then the number of elements in the array. Passes, pass the elements of the array one by one in the desired order to sort the elements of array.

Input – Enter the size of the array -5
Array is -23   53   1   25   65

Output
Array in the descending order is – 65   53   25   23   1

admin has written 2 articles

10 thoughts on “C program to sort Array in descending order

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>