C Program to Sort Array in Descending Order
if (arr[j] < arr[j+1]) swap(arr[j], arr[j+1]) inside a sorting loop.Examples
How to Think About It
Algorithm
Code
#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; }
Dry Run
Let's trace sorting the array [5, 3, 8, 1, 9] through the code
Initial array
arr = [5, 3, 8, 1, 9]
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]
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]
Third outer loop iteration (i=2)
Compare pairs: (8,5) no swap (5,9) swap -> arr = [8,9,5,3,1]
Fourth outer loop iteration (i=3)
Compare pairs: (8,9) swap -> arr = [9,8,5,3,1]
Sorted array
arr = [9,8,5,3,1]
| Iteration | Array 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
#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; }
#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; }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Bubble Sort (nested loops) | O(n^2) | O(1) | Small arrays, simple code |
| Selection Sort | O(n^2) | O(1) | Small arrays, easy to understand |
| qsort with comparator | O(n log n) | O(log n) stack | Large arrays, performance |