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:
Input:
1 2 |
Enter desired array size(1-100): 6 Please enter array elements(6) : 82 37 28 0 -12 -23 |
Output:
1 2 |
Original Array: 82 37 28 0 -12 -23 Sorted Array(Descending Order): 82 37 28 0 -12 -23 |
Prerequisites:
It is recommended to go through the following Arrays and Functions tutorials to better understand the sorting program.
- Introduction to C Arrays – How to Declare, Access, and Modify Arrays
- 2D arrays in C with Example programs
- How to pass arrays to functions in C
- Functions in C Language
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
- We have array values[] array with size number of variables.
-
for i = 0 to size-1
-
for j = 0 to size-i
- if values[i] < values[j+1]
- swap(values[i], values[i+1])
- if values[i] < values[j+1]
-
for j = 0 to size-i
- 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):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void bubbleSort(int values[], int size) { for i = 0 to size-1 { for j = 0 to size-i { if values[i] < values[j+1] { swap(values[i], values[i+1]) } } } } |
📢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:
- Declare the array values with max size of 100. Also declare supporting variables like i, j, size, and temp variables.
- Prompt the user to provide the desired array size and store it in size variable.
- 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.
- 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
- 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 j – j++
- Nested for loop, Iterate from j=0 till the size-i
- increment the variable i – i++
- Start the first for loop, Traverse from i=0 till the i<size-1
- Once the above two for loops are completed, Then the values array will be sorted in descending order.
- 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
- 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
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
/* Program to sort the numbers using bubble sort Author: SillyCodes.com */ #include <stdio.h> int main() { // declare the 'values' array int values[100]; int i, j, size, temp; printf("Enter desired array size(1-100): "); scanf("%d", &size); // User input for array elements printf("Please enter array elements(%d) : ", size); for(i = 0; i < size; i++) { // printf("values[%d] : ", i); scanf("%d", &values[i]); } // Print the original array printf("Original Array: "); for (i = 0; i < size; i++) { printf("%d ", values[i]); } printf("\n"); // Sort the array in descending order // We will use the bubble sort for (i = 0; i < size - 1; i++) { for (j = 0; j < size - i - 1; j++) { if (values[j] < values[j + 1]) { // Swap the elements using 'temp' variable temp = values[j]; values[j] = values[j + 1]; values[j + 1] = temp; } } } // Print the sorted array printf("Sorted Array(Descending Order): "); for (i = 0; i < size; i++) { printf("%d ", values[i]); } printf("\n"); return 0; } |
Program Output:
Compile and Run the Program using GCC compiler.
Test 1:
1 2 3 4 5 6 7 |
$ gcc sort-array-desc.c $ ./a.out Enter desired array size(1-100): 10 Please enter array elements(10) : 23 45 12 77 83 -34 0 12 98 6 Original Array: 23 45 12 77 83 -34 0 12 98 6 Sorted Array(Descending Order): 98 83 77 45 23 12 12 6 0 -34 $ |
Test 2:
1 2 3 4 5 6 7 8 9 10 11 |
$ ./a.out Enter desired array size(1-100): 6 Please enter array elements(6) : 82 37 28 0 -12 -23 Original Array: 82 37 28 0 -12 -23 Sorted Array(Descending Order): 82 37 28 0 -12 -23 $ ./a.out Enter desired array size(1-100): 4 Please enter array elements(4) : 82 29 -34 -8 Original Array: 82 29 -34 -8 Sorted Array(Descending Order): 82 29 -8 -34 $ |
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.
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
/* Program to sort the numbers in Descending order using bubble sort Author: SillyCodes.com */ #include <stdio.h> void sortDescending(int values[], int size); /** * @brief - Read the user input and update the 'values' array * * @param values - 'values' array * @param size - size of the 'values' array */ void read(int values[], int size) { int i; // User input for array elements printf("Please enter array elements(%d) : ", size); for(i = 0; i < size; i++) { // printf("values[%d] : ", i); scanf("%d", &values[i]); } } /** * @brief - Display the 'values' array * * @param values - Array * @param size - Array size */ void display(int values[], int size) { int i; for(i = 0; i < size; i++) { printf("%d ", values[i]); } printf("\n"); } int main() { // declare the 'values' array int values[100]; int i, j, size, temp; printf("Enter desired array size(1-100): "); scanf("%d", &size); // take user input read(values, size); // print original array printf("Original Array: "); display(values, size); // call 'sortDescending' function sortDescending(values, size); // Print sorted Array printf("Sorted Array(Descending Order): "); display(values, size); return 0; } /** * @brief - Sort the values array in descending order * * @param values - values array * @param size - size of the array */ void sortDescending(int values[], int size) { int i, j, temp; // Sort the array in descending order // We will use the bubble sort for (i = 0; i < size - 1; i++) { for (j = 0; j < size - i - 1; j++) { if (values[j] < values[j + 1]) { // Swap the elements using 'temp' variable temp = values[j]; values[j] = values[j + 1]; values[j + 1] = temp; } } } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ gcc sort-array-desc-func.c $ ./a.out Enter desired array size(1-100): 10 Please enter array elements(10) : 27 45 54 87 96 -24 0 -54 32 12 Original Array: 27 45 54 87 96 -24 0 -54 32 12 Sorted Array(Descending Order): 96 87 54 45 32 27 12 0 -24 -54 $ ./a.out Enter desired array size(1-100): 6 Please enter array elements(6) : 87 98 41 -56 -32 0 Original Array: 87 98 41 -56 -32 0 Sorted Array(Descending Order): 98 87 41 0 -32 -56 $ ./a.out Enter desired array size(1-100): 4 Please enter array elements(4) : 1 2 3 4 Original Array: 1 2 3 4 Sorted Array(Descending Order): 4 3 2 1 $ |
Related Programs:
- C Program to generate Prime numbers up to N
- C Program to Calculate Nth Prime Number
- C Program Check Prime Number [Mutliple Methods]
- C Program to Convert Decimal Number to Binary Number
- C Program to Convert Binary Number to Decimal Number
- C Program to Convert Octal Number to Decimal Number
- C Program to Calculate the Factorial of Number
- C Program to Calculate the Power of Number
- C Program to Check Number is Power of 2
- C Program to find power of number without Loops
- C Program to Find all Factors of a Number
- C Program to find all Prime Factors of number
- C Program to Calculate the GCD or HCF of Two Number
- C Program to Calculate the LCM of two Numbers
- C Program to Check Palindrome Number
- C Program to Check 3-Digit and N-Digit Armstrong Number