C program demonstrating Bubble Sort algorithm to sort integer arrays in ascending order, ideal for learning sorting basics in standard C environments.
#include <stdio.h>
// Function to sort an integer array using Bubble Sort
void bubbleSort(int arr[], int n) {
int i, j, temp;
// Outer loop for each element of the array
for (i = 0; i < n - 1; i++) {
// Inner loop to compare adjacent elements
for (j = 0; j < n - i - 1; j++) {
// If the current element is greater than the next, swap them
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// Function to print the elements of the array
void printArray(int arr[], int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
// Definition of an unsorted integer array
int array[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(array) / sizeof(array[0]);
printf("Original array: \n");
printArray(array, n);
// Sorting the array
bubbleSort(array, n);
printf("Sorted array: \n");
printArray(array, n);
return 0;
}Software: Standard C language (compatible with C99 and later).
Software: No external libraries required, uses only stdio.h for input/output.
Configuration: No special configuration required.
Hardware: No specific hardware required, can run on any PC or microcontroller with a C compiler.
Arduino: If you want to use it on Arduino, connect the device to the PC via USB to upload the program (no specific pins are needed for this example).