// Define the array to be sorted
int arr[] = {23, 45, 12, 9, 56, 34, 78, 67, 10, 3}; // Initial array
const int arrLength = 10; // Length of the array
void setup() {
// Start the Serial communication at 9600 baud rate
Serial.begin(9600);
// Display the original array
Serial.println("Original Array:");
printArray();
// Perform Bubble Sort with results printed after each pass
bubbleSort();
}
void loop() {
// Nothing to do in the loop
}
// Function to perform Bubble Sort and print the array after each pass
void bubbleSort() {
for (int i = 0; i < arrLength - 1; i++) {
Serial.print("Pass ");
Serial.print(i + 1);
Serial.println(":");
for (int j = 0; j < arrLength - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
// Print the array after each pass
printArray();
}
}
// Function to print the array
void printArray() {
for (int i = 0; i < arrLength; i++) {
Serial.print(arr[i]);
Serial.print(" ");
}
Serial.println(); // Move to the next line
}