C program to sort array in descending order is to sort or arrange the given array in descending order.

What is sorting
It is the technique to sort an array or arrange data in some order either in ascending order or descending order.

Example :-
array [] = 20, 10, 30
Sorted list in descending order is
array [] = 30, 20, 10

Algorithm to sort array in descending order

  1. Declare variables. N, a[]
  2. Take array input from a user. a[N]
  3. Traverse array
    For i=0 to end
    If a[i] < a[i+1]
    swap a[i] and a[i+1]
    Else continue
    If a[i]== end
    stop
  4. Print sorted array

C program to sort array in descending order

#include<stdio.h>
main()
{
int i,j,num,temp;
printf("Enter number of elements\n");
scanf("%d",&num);
int a[num];
printf("Enter numbers\n");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
}

for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("descending oreder of given numbers is\n");
for(i=0;i<num;i++)
{
printf("%d\n",a[i]);
}
}

Output :

C program to sort array in descending order

Write A Comment