Skip to content

C program to sort Array in descending order

by admin on March 29th, 2012

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

9 Comments
  1. instead of initizaling the array to size [100], why not initialize the array to size [n] after user inputs it?

    • size of an array should only be in integer guys… that’s it…. it cannot be used as
      [n]

  2. Ideal Information, appreciate it. When it comes to customers which usually expect to get in shape really fast, consider our website for the best means by which to burn excess fat efficiently.

  3. Aritra Ghosh permalink

    There is a bug in the code..
    for(i=0;i<n;i++)
    not "for(i=1;i<=n;i++)"
    as array is default initialized to zero.. :)

  4. mandela permalink

    perfect answere

  5. sudheer permalink

    why should we give condition in for loop as “j<n-i" why dont just "j<n"

  6. tejas permalink

    thnx v much

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS