- How to sort an array in descending order
- How bubble sort algorithm works for descending order
- How to swap elements using a temporary variable
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to sort an array in descending order.
Sorting an array in descending order means arranging elements from largest to smallest. This is useful in many real-world applications, such as:
- Ranking students by highest marks
- Listing products by highest price
- Displaying top scores in a game
- Sorting salaries from highest to lowest
💡 Key Point: The only difference between ascending and descending order sorting is the comparison condition. Instead of arr[j] > arr[j+1], we use arr[j] < arr[j+1] to sort in descending order.
C Program to Sort Array in Descending Order (Using Bubble Sort)
#include <stdio.h>
int main() {
int n, i, j, temp;
// Ask user for number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Declare array
int arr[n];
// Read elements into the array
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Bubble sort - sort in descending order
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(arr[j] < arr[j+1]) { // Compare for descending
// Swap arr[j] and arr[j+1]
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// Display sorted array
printf("\nArray in descending order: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements: 6 Enter 6 elements: 50 20 40 10 30 60 Array in descending order: 60 50 40 30 20 10
Another Example:
Enter the number of elements: 5 Enter 5 elements: 25 10 35 15 20 Array in descending order: 35 25 20 15 10
Example with Negative Numbers:
Enter the number of elements: 7 Enter 7 elements: -5 10 -2 8 0 -10 3 Array in descending order: 10 8 3 0 -2 -5 -10
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int n;— stores the number of elementsint i, j;— loop countersint temp;— temporary variable for swapping
- Get User Input: Prompts the user to enter the number of elements and then reads them into the array.
- Bubble Sort in Descending Order:
- Outer Loop:
for(i = 0; i < n-1; i++)— runs n-1 times - Inner Loop:
for(j = 0; j < n-i-1; j++)— compares adjacent elements - Comparison: If
arr[j] < arr[j+1], swap them (this puts larger elements first) - After each pass, the smallest element "bubbles down" to the end
- Outer Loop:
- Display Result: Prints the sorted array in descending order.
- Return:
return 0;indicates successful program execution.
📝 Note: The only difference from ascending order is the comparison operator: < instead of >. This simple change reverses the sorting order.
Bubble Sort Algorithm for Descending Order
Step-by-step explanation of descending bubble sort:
- Start with the first element
- Compare the current element with the next element
- If the current element is smaller than the next element, swap them
- Move to the next element and repeat step 2-3
- After one complete pass, the smallest element is at the end
- Repeat the process for the remaining elements (excluding the last)
- Stop when all elements are sorted in descending order
Visual Example
Let's see how descending bubble sort works on the array [50, 20, 40, 10, 30]:
Pass 1:
[50, 20, 40, 10, 30] → No swap (50 > 20) → [50, 20, 40, 10, 30]
[50, 20, 40, 10, 30] → Swap 20 and 40 → [50, 40, 20, 10, 30]
[50, 40, 20, 10, 30] → No swap (20 > 10) → [50, 40, 20, 10, 30]
[50, 40, 20, 10, 30] → Swap 10 and 30 → [50, 40, 20, 30, 10]
Result: [50, 40, 20, 30, 10] — 10 is now in correct position
Pass 2:
[50, 40, 20, 30, 10] → No swap (50 > 40) → [50, 40, 20, 30, 10]
[50, 40, 20, 30, 10] → Swap 20 and 30 → [50, 40, 30, 20, 10]
Result: [50, 40, 30, 20, 10] — 20 is now in correct position
Pass 3:
[50, 40, 30, 20, 10] — No swaps needed, array is sorted!
Optimized Descending Bubble Sort
We can optimize bubble sort by adding a flag to check if any swaps were made in a pass. If no swaps were made, the array is already sorted and we can break early.
#include <stdio.h>
int main() {
int n, i, j, temp;
int swapped;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Optimized descending bubble sort
for(i = 0; i < n-1; i++) {
swapped = 0;
for(j = 0; j < n-i-1; j++) {
if(arr[j] < arr[j+1]) { // Compare for descending
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = 1;
}
}
// If no swaps were made, array is already sorted
if(swapped == 0) {
break;
}
}
printf("\nArray in descending order: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Ascending vs Descending Order
| Feature | Ascending Order | Descending Order |
|---|---|---|
| Arrangement | Smallest to Largest | Largest to Smallest |
| Comparison | arr[j] > arr[j+1] |
arr[j] < arr[j+1] |
| Example | 10, 20, 30, 40, 50 | 50, 40, 30, 20, 10 |
| Use Case | Student grades (low to high) | Top scores, highest salaries |
💻 Practice Exercise
Challenge 1: Write a program that allows the user to choose between ascending or descending order before sorting.
Challenge 2: Sort an array of strings in descending alphabetical order.
🔍 Click to Show Solution for Challenge 1
#include <stdio.h>
int main() {
int n, i, j, temp;
int choice;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("\nChoose sorting order:\n");
printf("1. Ascending\n");
printf("2. Descending\n");
printf("Enter your choice: ");
scanf("%d", &choice);
for(i = 0; i < n-1; i++) {
for(j = 0; j < n-i-1; j++) {
if(choice == 1) {
// Ascending
if(arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
} else {
// Descending
if(arr[j] < arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
printf("\nSorted array: ");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Frequently Asked Questions
1. How do you sort an array in descending order in C?
Use the same bubble sort algorithm but change the comparison condition from arr[j] > arr[j+1] to arr[j] < arr[j+1]. This puts larger elements first.
2. What is the difference between ascending and descending sort?
Ascending sorts from smallest to largest (1, 2, 3, 4). Descending sorts from largest to smallest (4, 3, 2, 1).
3. Can I use the same array for both ascending and descending sort?
Yes, you can sort the same array in either order. The original array will be modified. If you need to keep the original, make a copy first.
4. Which sorting algorithm is best for descending order?
Any sorting algorithm can be adapted for descending order by changing the comparison. Quick Sort and Merge Sort are more efficient for large arrays.
5. How does bubble sort work for descending order?
Bubble sort compares adjacent elements and swaps them if they are in the wrong order. For descending, it swaps when the first element is smaller than the second, moving larger elements to the front.
💡 Tip: To switch between ascending and descending, simply change the comparison operator from > to < (or vice versa).