C Program to Sort N Numbers in Descending Order

Sorting in descending order means arranging elements from largest to smallest. The only difference from ascending bubble sort is the direction of the comparison: ascending uses arr[j] > arr[j+1] (swap when left is larger), while descending uses arr[j] < arr[j+1] (swap when left is smaller). This program fixes the original which used void main, a hard-coded 30-element limit, and broken \n in format strings.

How Descending Bubble Sort Works (pass 1 of [3,1,4,1,5])

j Compare Swap? Array after
0 3 < 1? No No 3, 1, 4, 1, 5
1 1 < 4? Yes Yes 3, 4, 1, 1, 5
2 1 < 1? No No 3, 4, 1, 1, 5
3 1 < 5? Yes Yes 3, 4, 1, 5, 1

After pass 1: the smallest element (1) has bubbled to the end. After all passes: 5, 4, 3, 1, 1 ✓

C Program: Sort Numbers in Descending Order

/* Sort N integers in descending order using bubble sort
 * Compile: gcc -ansi -Wall -Wextra descend.c -o descend */
#include <stdio.h>
#define MAX 100

int main(void)
{
    int num[MAX];
    int i, j, temp, n;

    printf("How many numbers (1-%d)? ", MAX);
    if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
        printf("Enter a value between 1 and %d.\n", MAX);
        return 1;
    }

    printf("Enter %d numbers:\n", n);
    for (i = 0; i < n; i++) {
        if (scanf("%d", &num[i]) != 1) {
            printf("Invalid input.\n");
            return 1;
        }
    }

    /* Bubble sort: swap if left < right to get descending order */
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - 1 - i; j++) {
            if (num[j] < num[j + 1]) {
                temp       = num[j];
                num[j]     = num[j + 1];
                num[j + 1] = temp;
            }
        }
    }

    printf("Sorted in descending order:\n");
    for (i = 0; i < n; i++) {
        printf("%d", num[i]);
        if (i < n - 1) printf(", ");
    }
    printf("\n");

    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra descend.c -o descend
./descend

Sample Output

How many numbers (1-100)? 5
Enter 5 numbers:
3 1 4 1 5
Sorted in descending order:
5, 4, 3, 1, 1

How many numbers (1-100)? 4
Enter 4 numbers:
-2 7 0 3
Sorted in descending order:
7, 3, 0, -2

Ascending vs Descending — One Change

Order Comparison Effect
Ascending if (num[j] > num[j+1]) Largest elements bubble to end
Descending if (num[j] < num[j+1]) Smallest elements bubble to end

Time and Space Complexity

Case Time Space
Best (already sorted) O(n²) — or O(n) with early-exit flag O(1)
Average O(n²) O(1)
Worst (reverse sorted) O(n²) O(1)

Code Explanation

  • Outer loop index i — counts completed passes. After each pass, the i smallest elements have settled at the end, so the inner loop only needs to go to n-1-i. This avoids redundant comparisons for elements already in place.
  • Inner loop: j < n-1-i — the upper bound shrinks by 1 each pass. After pass 0, the last position is correct; after pass 1, the last two are correct; and so on.
  • Three-variable swap — always use a temporary variable. The XOR and arithmetic swap tricks have edge cases (XOR zeroes both values when pointers alias; arithmetic overflows). The temp-variable swap is always correct.

What This Program Teaches

  • Bubble sort direction — flipping one comparison operator converts ascending to descending. Understanding this abstraction helps when you need custom sort criteria: sort by absolute value, sort strings by length, sort structs by a field — all require the same structural change.
  • Inner loop bound reduction — after each outer pass, the inner bound shrinks. Without this, the algorithm still produces correct results but performs O(n²/2) extra comparisons. The optimisation halves the constant factor.
  • When to use bubble sort — bubble sort is rarely the right choice for large inputs (use qsort from stdlib.h or merge sort). It is useful for educational purposes, for very small arrays (<10 elements), or when you need a simple in-place sort with minimal code.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>