Problem statement:- Program to Sort an array in Descending order.
Example:-
Input : Given
size=5
arr[]=[4,5,7,3,9]
Output:
arr[]=[9,7,5,4,3]
Data requirement:-
Input Data:- size, arr
Output Data:-arr
Additional Data:- i, j, temp
Program in C
Input Data:- size, arr
Output Data:-arr
Additional Data:- i, j, temp
Program in C
Here is the source code of the C Program to Sort an array in Descending order.
Code:
#include<stdio.h>
main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size];
int i,temp,j;
printf("Enter the Element of the array:\n");
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]<=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
printf("\nAfter Decreasing order sort Array Elements are:");
for(i=0;i<size;i++)
{
printf("%d ",arr[i]);
}
}
Enter the size of the array:5
Enter the Element of the array:
4 5 7 3 9
After Decreasing order sort Array Elements are:9 7 5 4 3
Program in C++
Here is the source code of the C++ Program to Sort an array in Descending order.
Code:
#include<iostream>
using namespace std;
main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size];
int i,temp,j;
cout<<"Enter the Element of the array:\n";
for(i=0;i<size;i++)
{
cin>>arr[i];
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]<=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
cout<<"\nAfter Decreasing order sort Array Elements are:";
for(i=0;i<size;i++)
{
cout<<arr[i]<<" ";
}
}
Enter the size of the array:4
Enter the Element of the array:
5 10 6 12
After Decreasing order sort Array Elements are:12 10 6 5
Program in Java
Here is the source code of the Java Program to Sort an array in Descending order.
Code:
import java.util.Scanner;
public class SortingDecreasingOrder {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int i,j,temp,size;
size=sc.nextInt();
int arr[ ]=new int[size];
System.out.println("Enter the Element of the array:");
for(i=0;i<size;i++)
{
arr[i]=sc.nextInt();
}
System.out.print("\nArray elements are:");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
if(arr[i]<=arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}}
System.out.print("\nAfter Decreasing order sort Array Elements are:");
for(i=0;i<size;i++)
{
System.out.print(arr[i]+" ");
}
sc.close();
}}
Enter the size of the array:
6
Enter the Element of the array:
7 9 2 4 1 8
Array elements are:7 9 2 4 1 8
After Decreasing order sort Array Elements are:9 8 7 4 2 1
Program in Python
Here is the source code of the Python Program to Sort an array in Descending order.
Code:
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
print("Before sorting array elements are:")
for i in range(0,size):
print(arr[i],end=" ")
for i in range(0,size):
for j in range(i+1, size):
if arr[i] <= arr[j]:
temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
print("\nAfter Decreasing order sort Array Elements are:")
for i in range(0, size):
print(arr[i],end=" ")
Enter the size of the array: 3
Enter the Element of the array:
5
9
2
Before sorting array elements are:
5 9 2
After Decreasing order sort Array Elements are:
9 5 2
0 Comments
Please do not Enter any spam link in the comment box