C Program to sort an array in descending order


March 16, 2022, Learn eTutorial
1435

What is sorting in C?

In this program, we need to sort the given array in descending order such that elements will be arranged from largest to smallest. Sorting is a process of rearranging the numbers in an array or a list in some order, which will be ascending or descending order. Here you will see how to sort an array in descending order sorting is handy and has a lot of application-level usage in real life. In c programming, there are different kinds of sorting methods used depends on the technique used to sort the elements. Each mode uses a different type of style, and all have various complexities for both the best case and worst case. Some of the sorting ways in c are:

  • Bubble Sort
  • Selection Sort
  • Merge Sort
  • Insertion Sort
  • Quick Sort
  • Heap Sort

How do arrange the numbers in descending order?.

Descending order means arranging the numbers from biggest to smallest. In this c program, we need to arrange the elements of an array in descending order. For example, if we have a set of numbers like "1, 5, 3, 8, 7, 6". We need to change it to 8, 7, 6, 5, 3,1" that is sorted in descending order.

In this c program, we accept the count of the numbers the user wants to sort. Then we accept the numbers from the user using a for loop and save it into the array number[]. By using a nested for loop, check each element of the array with the next number; if the number is smaller than the following number, then we will swap the numbers using a temporary variable. Finally, display the array, which is sorted in descending order using for loop.

In this program, for loop is used. Syntax of for loop is given by

for (initializationStatement; testExpression; updateStatement)

     {
          // codes
     }

Here the initialization statement is executed only once. Initially, test expression is evaluated. If the test expression is False, we terminate the loop. But if the test expression is True, then execute code inside the for loop and update the expression. This process continues until the test expression is False. This type of loop is commonly used when the number of iterations is already known.

ALGORITHM

STEP 1:Include the header files to use the built-in functions in the C program.

STEP 2: Declare the integer variables i, j, a, n, and the Array number[30].

STEP 3: Read the value of N into the variable n.

STEP 4: Read the numbers into the array number[i] using for loop.

STEP 5: Set i=0.

STEP 6: Check 'i'

STEP 7: set j=i+1.

STEP 8: Check if number[i].

STEP 9: Increment 'j' by 1 and do step 7.

STEP 10: Increment 'i' by 1 do step 6.

STEP 11: Display the numbers arranged in Descending order as the numbers in the Array number[i] using for loop.

C Source Code

                                          #include <stdio.h>

void main() {
  int number[30];
  int i, j, a, n;
  printf("Enter the value of N\n");
  scanf("%d", & n);
  printf("Enter the numbers \n");
  for (i = 0; i < n; ++i)
    scanf("%d",& number[i]);
  for (i = 0; i < n; ++i) /* sorting begins ...*/ {
    for (j = i + 1; j < n; ++j) {
      if (number[i] < number[j]) {
        a = number[i];
        number[i] = number[j];
        number[j] = a;
      }
    }
  }
  printf("The numbers arranged in descending order are given below\n");
  for (i = 0; i < n; ++i) {
    printf("%d\n", number[i]);
  }
} /* End of main() */
                                      

OUTPUT

Enter the value of N
6

Enter the numbers
10
35
67
100
42
688

The numbers arranged in descending order are given below
688
100
67
42
35
10