Sort Array in Descending Order in C Language


Program Description:

In this program, We are going to write a program to Sort Array in Descending Order in C programming language. The program accepts a input array from the user and sorts the provided in the Desending order. We are going to use the bubble sort to sort the array.

Example Input and Output:




It is recommended to go through the following Arrays and Functions tutorials to better understand the sorting program.

Bubble Sort Algorithm to Sort Array in Descending Order:

Bubble sort is a basic sorting algorithm that iterates through the array elements repeatedly, compares adjacent elements, and swaps them if any of the elements are out of order.

The above process is repeated until no swaps are required, at which point the Array is considered to be sorted.

Here is the algorithm for the bubble sort to sort the elements of the array in Descending Order

  1. We have array values[] array with size number of variables.
  2. for i = 0 to size-1
    • for j = 0 to size-i
      • if values[i] < values[j+1]
        • swap(values[i], values[i+1])
  3. Once the above loop is terminated, values[] array will be sorted in ascending order.

Pseudo code for the bubble sort in c language ( Descending Order):

📢The time complexity of the bubble sort it O(n2). So it will be good for small arrays, But not suitable for large arrays.

Sort Array in Descending Order in C using Bubble Sort Explanation:

  1. Declare the array values with max size of 100. Also declare supporting variables like i, j, size, and temp variables.
  2. Prompt the user to provide the desired array size and store it in size variable.
  3. Use a For loop to Iterate through the array and update the array elements using the scanf function.
    • Start the loop from zero(0) index and go til the size-1
    • Update the values[i] element with a user-provided number.
  4. Print the original array using another for loop.
    • Loop from i=0 to i<size and At each iteration use printfto print the values[i] element
  5. Now, We need to sort the values array using the bubble sort. Bubble sort uses two for loops and swaps the adjacent elements if they are in the wrong order. Here we need to sort the array in Descending order.
    • Start the first for loop, Traverse from i=0 till the i<size-1
      • Nested for loop, Iterate from j=0 till the size-i
        • At Each iteration, Check if the values[j+1] is greater than the values[j]. If it is true, Then Swap the values[j] and values[j+1] variables. – if (values[j] < values[j + 1])
        • We are using a temporary variable temp to swap the elements. We can also use other methods like Swapping without using temporary variables
      • Increment the variable jj++
    • increment the variable ii++
  6. Once the above two for loops are completed, Then the values array will be sorted in descending order.
  7. Print the sorted Array on the console using another For loop.
    • Loop from i=0 to i<size and At each iteration use printfto print the values[i] element
  8. Stop the program.

Program to Sort Array in Descending Order in C Langauge:

Sort array elements in Descending order using bubble sort in c programming language

Program Output:

Compile and Run the Program using GCC compiler.

Test 1:


Test 2:

As we can see from the above output, The program is sorting the array elements correctly.

Sort Array in Descending Oder in C using Functions:

In the above program, We have used sinlge main() function to write complete program. If you observe there are few parts of the program which can be moved to a function. So let’s create few functions to do a specific task like reading input to array, printing array, etc.

Here is the rewritten version of the Array elements sorting program with user defined functions.

display() Function:

The display() function takes two formal arguments named values array and its size. It simply displays the all elements of the array on the console.

Prototype of the display() function – void display(int values[], int size)

read() Function:

read() function is used to read the user input and update the array elements. The read() function also takes two formal arguments values[] array and the size of values array.

Prototype details of the read() function – void read(int values[], int size)

The read() function, updates the values array as values array passed as the address, So all the changes made in the read() function are visible in the main() function.

bubbleSort() Function:

This is the function, where the actual sorting happens. The bubbleSort() function also takes two formal arguments similar to the above two functions – values[] array and size.

Prototype of the bubbleSort() function – void bubbleSort(int values[], int size)

The bubbleSort function uses the bubble sort algorithm to compare and swap the elements and creates a sorted array. Here we are sorting the elements in the descending order. The bubbleSort function also changes the input values array(Array is passed as address/reference). So all changes made in the bubbleSortfunction are visible in the main() function.

Program Output:

Let’s compile and run the program and observe the output.


Related Programs:

  1. C Program to generate Prime numbers up to N
  2. C Program to Calculate Nth Prime Number
  3. C Program Check Prime Number [Mutliple Methods]
  4. C Program to Convert Decimal Number to Binary Number
  5. C Program to Convert Binary Number to Decimal Number
  6. C Program to Convert Octal Number to Decimal Number
  7. C Program to Calculate the Factorial of Number
  8. C Program to Calculate the Power of Number
  9. C Program to Check Number is Power of 2
  10. C Program to find power of number without Loops
  11. C Program to Find all Factors of a Number
  12. C Program to find all Prime Factors of number 
  13. C Program to Calculate the GCD or HCF of Two Number
  14. C Program to Calculate the LCM of two Numbers
  15. C Program to Check Palindrome Number
  16. C Program to Check 3-Digit and N-Digit Armstrong Number


Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply