C program to sort an array in descending order

Sorting an array in descending order means arranging elements from highest to lowest value. This is commonly achieved using various sorting algorithms, with bubble sort being one of the simplest approaches.

Syntax

// Bubble sort for descending order
for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
        if (arr[i] < arr[j]) {
            // Swap elements
        }
    }
}

Array Declaration

An array is a collection of elements of the same data type stored in contiguous memory locations. The syntax for declaring an array is −

datatype array_name[size];

For example:

int numbers[10];    // Array of 10 integers
float marks[50];    // Array of 50 float values

Example: Bubble Sort in Descending Order

The following program demonstrates sorting an array in descending order using bubble sort algorithm −

#include <stdio.h>

int main() {
    int arr[20];
    int i, j, temp, n;
    
    printf("Enter number of elements: ");
    scanf("%d", &n);
    
    printf("Enter %d elements:<br>", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
    
    // Bubble sort for descending order
    for (i = 0; i < n - 1; i++) {
        for (j = i + 1; j < n; j++) {
            if (arr[i] < arr[j]) {
                // Swap elements
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    
    printf("Array in descending order:<br>");
    for (i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("<br>");
    
    return 0;
}
Enter number of elements: 5
Enter 5 elements:
23 45 12 67 34
Array in descending order:
67 45 34 23 12 

How It Works

The bubble sort algorithm compares adjacent elements and swaps them if they are in the wrong order:

  • Outer loop: Controls the number of passes through the array
  • Inner loop: Compares and swaps elements within each pass
  • Condition: if (arr[i] < arr[j]) ensures larger elements move to the front
  • Time Complexity: O(n²) in worst case

Conclusion

Sorting arrays in descending order using bubble sort is straightforward and effective for small datasets. The algorithm repeatedly compares adjacent elements and swaps them to achieve the desired order.

Updated on: 2026-03-15T14:03:24+05:30

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements