C Program to Sort the Elements of an Array in Ascending Order - Source Code, Output & Execution Flow

C Program to Sort the Elements of an Array in Ascending Order

This C program sorts array elements in ascending order.

#include <stdio.h>
int main() {
    int a[] = {4, 2, 9, 1, 7};
    int n = 5, i, j, temp;
    for (i = 0; i < n - 1; i++)
        for (j = i + 1; j < n; j++)
            if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; }
    for (i = 0; i < n; i++) printf("%d ", a[i]);
    return 0;
}
1 2 4 7 9

In this program, the input is read first when required, the core logic is applied step by step, and the final result is displayed using printf(). Follow the execution flow beside the code to understand how each statement contributes to the output.