#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
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;
}
}
int main() {
int arr[] = {54, 17, 23, 25, 27, 13, 52};
int n = sizeof(arr)/sizeof(arr[0]);
int i;
printf("Original array: \n");
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
bubbleSort(arr, n);
printf("Sorted array: \n");
for (i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
This program first defines a bubbleSort function, where the actual sorting algorithm is implemented. The outer loop runs n-1 times (where n is the number of elements in the array) ensuring the sorting process is repeated enough times. The inner loop runs for each pair of adjacent elements, swapping them if they are not in order. Note that with each pass through the array, the largest unsorted element "bubbles up" to its correct position at the end of the array, so the range of the inner loop can decrease accordingly.