C Program to Sort an Array using Bubble Sort
This C program sorts an array using bubble sort.
#include <stdio.h>
int main() {
int a[] = {5, 1, 4, 2, 8};
int n = 5, i, j, temp;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (a[j] > a[j + 1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; }
printf("Sorted array: ");
for (i = 0; i < n; i++) printf("%d ", a[i]);
return 0;
}
Sorted array: 1 2 4 5 8
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.
