C Program to Sort an Array Using Selection Sort - Source Code, Output & Execution Flow

C Program to Sort an Array Using Selection Sort

This C program sorts an array using selection sort.

#include <stdio.h>
int main() {
    int a[] = {64, 25, 12, 22, 11};
    int n = 5, i, j, min, temp;
    for (i = 0; i < n - 1; i++) {
        min = i;
        for (j = i + 1; j < n; j++) if (a[j] < a[min]) min = j;
        temp = a[i]; a[i] = a[min]; a[min] = temp;
    }
    for (i = 0; i < n; i++) printf("%d ", a[i]);
    return 0;
}
11 12 22 25 64

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.