C program to sort array in descending order; Through this tutorial, we will learn how to sort array in descending order using the standard method and function in c programs.
Programs to Sort Array in Descending Order in C
Use the following programs to sort array in descending order using the standard method and function in c:
- C Program to Sort Array in Descending Order using Standard Method
- C Program to Sort Array in Descending Order using Function
C Program to Sort Array in Descending Order using Standard Method
#include <stdio.h>
int main()
{
int a[10000],i,n,j,temp;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\narray elements in descending order:\n ");
for(i=0; i<n; i++)
{
printf("%d ",a[i]);
}
}
The output of the above c program; as follows:
Enter size of the array : 5 Enter elements in array : 1 2 3 4 5 array elements in descending order: 5 4 3 2 1
C Program to Sort Array in Descending Order using Function
#include <stdio.h>
int sort(int *a,int n)
{
int i,j,temp;
for(i=0; i<n-1; i++)
{
for(j=0; j<n-i-1; j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
print(int *a,int n)
{
int i;
for(i=0; i<n; i++)
{
printf("%d ",a[i]);
}
}
int main()
{
int a[10000],i,n,key;
printf("Enter size of the array : ");
scanf("%d", &n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
sort(a,n);
print(a,n);
}
The output of the above c program; as follows:
Enter size of the array : 5 Enter elements in array : 5 6 7 8 9 9 8 7 6 5