In this tutorial you will learn about the C Program to Sort Array in Descending Order and its application with practical example.
C Program to Sort Array in Descending Order
In this tutorial, we will learn to create a C program that will Sort Array in Descending Order using C programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in C programming.
- Arithmetic operations in C Programming.
What is An array?
The array is a collection of similar data types. An array can store multiple values with different indexes in memory by using a single variable. The array can be both single as well as multidimensional.
Sort Array in Descending Order.
In this tutorial, we will sort the array in descending order from the biggest to the smallest value of the array.
1 2 3 4 |
<strong>For example :- array be a[5] = { 5 , 1 , 2 , 3 , 4 } Then the sorted array will be a[5] = { 5 , 4 , 3 , 2 , 1 }</strong> |
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input size of the array. 3. Taking the elements in the array. 4. Sending that array to the for loop for sorting. 5. Print the Sorted element of array in Descending order. 6. End the program. |
Program description to Sort Array in Descending Order.
In this program, we will first take the input array size and the elements from the user. Then we will sort all the elements of the array in descending order. At last, we will print that sorted array elements.
With the help of this program, we can Sort Array in Descending Order.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
/* * * C Program to Sort Array in Descending Order * * */ #include <stdio.h> void main () { //declaring the required variables for the prorgam int Arry[30]; int i, j, a, n; //i = it will hold the integer value for the loop //j = it will hold the integere value for the loop //a = it will hold the integer value //n = it will hold the integer value //Arry = it will hold the integer value //Taking the input Arry of elements of the array printf("Enter the value of N\n"); scanf("%d", &n); //Taking the array elements from the user printf("Enter the Array's Elements \n"); for (i = 0; i < n; ++i) scanf("%d", &Arry[i]); /* sorting begins ... */ //Sorting the array elements in the descending order for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) { if (Arry[i] < Arry[j]) { a = Arry[i]; Arry[i] = Arry[j]; Arry[j] = a; } } } //Printing the arranged array in ascending order. printf("The Arrys arranged in descending order are given below\n"); for (i = 0; i < n; ++i) { printf("%d\n", Arry[i]); } } |
Output:-
In the above program, we have first initialized the required variable.
- j = it will hold the integer value.
- arry[] = it will hold the integer value of the input elements.
- i = it will hold the integer value.
- n = it will hold the integer value of the input.
Input number of elements from the user.
Program Logic Code.
Printing output in Descending order.