0
0
CProgramBeginner · 2 min read

C Program to Sort Array in Descending Order

To sort an array in descending order in C, use nested loops to compare elements and swap them if the first is smaller than the second, like if (arr[j] < arr[j+1]) swap(arr[j], arr[j+1]) inside a sorting loop.
📋

Examples

Input5 3 8 1 9
Output9 8 5 3 1
Input10 10 10
Output10 10 10
Input1
Output1
🧠

How to Think About It

To sort an array in descending order, think of comparing each element with the others and putting the bigger numbers first. You can do this by checking pairs of numbers and swapping them if the first is smaller, repeating this until the whole list is sorted from largest to smallest.
📐

Algorithm

1
Get the size of the array and the array elements from the user.
2
Use two loops to compare each pair of elements in the array.
3
If the current element is smaller than the next element, swap them.
4
Repeat the process until no more swaps are needed and the array is sorted in descending order.
5
Print the sorted array.
💻

Code

c
#include <stdio.h>

int main() {
    int n, i, j, temp;
    printf("Enter 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]);
    }

    for(i = 0; i < n - 1; i++) {
        for(j = 0; j < n - i - 1; j++) {
            if(arr[j] < arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    printf("Sorted array in descending order:\n");
    for(i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    return 0;
}
Output
Enter number of elements: 5 Enter 5 elements: 5 3 8 1 9 Sorted array in descending order: 9 8 5 3 1
🔍

Dry Run

Let's trace sorting the array [5, 3, 8, 1, 9] through the code

1

Initial array

arr = [5, 3, 8, 1, 9]

2

First outer loop iteration (i=0)

Compare pairs and swap if left < right: (5,3) no swap (3,8) swap -> arr = [5,8,3,1,9] (3,1) no swap (1,9) swap -> arr = [5,8,3,9,1]

3

Second outer loop iteration (i=1)

Compare pairs: (5,8) swap -> arr = [8,5,3,9,1] (5,3) no swap (3,9) swap -> arr = [8,5,9,3,1]

4

Third outer loop iteration (i=2)

Compare pairs: (8,5) no swap (5,9) swap -> arr = [8,9,5,3,1]

5

Fourth outer loop iteration (i=3)

Compare pairs: (8,9) swap -> arr = [9,8,5,3,1]

6

Sorted array

arr = [9,8,5,3,1]

IterationArray State
Start[5, 3, 8, 1, 9]
i=0[5, 8, 3, 9, 1]
i=1[8, 5, 9, 3, 1]
i=2[8, 9, 5, 3, 1]
i=3[9, 8, 5, 3, 1]
💡

Why This Works

Step 1: Comparing elements

The program compares each pair of adjacent elements using if(arr[j] < arr[j + 1]) to find if the left element is smaller.

Step 2: Swapping elements

If the left element is smaller, it swaps the two elements to move the larger one forward, using a temporary variable temp.

Step 3: Repeating passes

The nested loops repeat this process multiple times until the entire array is sorted in descending order.

🔄

Alternative Approaches

Using qsort with custom comparator
c
#include <stdio.h>
#include <stdlib.h>

int compare_desc(const void *a, const void *b) {
    return (*(int*)b - *(int*)a);
}

int main() {
    int n;
    printf("Enter number of elements: ");
    scanf("%d", &n);
    int arr[n];
    printf("Enter %d elements:\n", n);
    for(int i = 0; i < n; i++) scanf("%d", &arr[i]);

    qsort(arr, n, sizeof(int), compare_desc);

    printf("Sorted array in descending order:\n");
    for(int i = 0; i < n; i++) printf("%d ", arr[i]);
    printf("\n");
    return 0;
}
This method uses the standard library's quicksort with a custom comparator for descending order, which is faster and cleaner for large arrays.
Selection sort descending
c
#include <stdio.h>

int main() {
    int n, i, j, max_idx, temp;
    printf("Enter 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]);

    for(i = 0; i < n - 1; i++) {
        max_idx = i;
        for(j = i + 1; j < n; j++) {
            if(arr[j] > arr[max_idx]) max_idx = j;
        }
        temp = arr[i];
        arr[i] = arr[max_idx];
        arr[max_idx] = temp;
    }

    printf("Sorted array in descending order:\n");
    for(i = 0; i < n; i++) printf("%d ", arr[i]);
    printf("\n");
    return 0;
}
Selection sort finds the maximum element each pass and places it at the front, which is simple but less efficient for large arrays.

Complexity: O(n^2) time, O(1) space

Time Complexity

The nested loops each run up to n times, making the time complexity O(n^2) because every element is compared with others.

Space Complexity

The sorting is done in-place using only a temporary variable for swapping, so space complexity is O(1).

Which Approach is Fastest?

Using the standard library's qsort with a custom comparator is faster (average O(n log n)) than bubble or selection sort (O(n^2)) for large arrays.

ApproachTimeSpaceBest For
Bubble Sort (nested loops)O(n^2)O(1)Small arrays, simple code
Selection SortO(n^2)O(1)Small arrays, easy to understand
qsort with comparatorO(n log n)O(log n) stackLarge arrays, performance
💡
Use a temporary variable to swap elements safely when sorting.
⚠️
Forgetting to swap elements correctly or using the wrong comparison operator causes incorrect order.