Here’s a coded demonstration which animates a bubble sort

Here’s a link which contains the executable files and code in c for download:
https://drive.google.com/drive/folders/1Uc8Hbr1tgQlf6XjMbn2h2MaIL9c86iXE?usp=drive_link
Here’s a the coded example using bubble sort to manipulate an integer array in C.
This is what the animation was based upon:
#include <stdio.h>
#include <stdlib.h>
void bubbleSort (int a [ ], int numItems);
void bubbleSort2 (int a [ ], int numItems);
void print(int a[], int numItems);
int main()
{
int myArray[] = {4, 3, 1, 2, -1};
// Calculate the number of items in the array
int numItems = sizeof(myArray) / sizeof(myArray[0]);
// Display the array
print(myArray, numItems);
//bubbleSort the array
bubbleSort2(myArray,numItems);
//Display the array after sorting
print(myArray, numItems);
return 0;
}
//bubble sort
void bubbleSort (int a [ ], int numItems)
{
int i, j, temp;
for(i=0;i<numItems -1; i++)
{
for (j = 0; (j<numItems-i-1); j++)
{
if (a[j]>a[j+1])
{
temp =a[j] ;
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
//bubble sort for debugging and demonstration
void bubbleSort2 (int a [ ], int numItems)
{
int i, j, temp;
for(i=0;i<numItems -1; i++)
{
printf("i=%d\n",i);
printf("\tInner loop , j values: ");
for (j = 0; (j<numItems-i-1); j++)
{
printf("%d",j);
if (a[j]>a[j+1])
{
temp =a[j] ;
a[j]=a[j+1];
a[j+1]=temp;
}
}
print(a,numItems);
printf("\n");
}
}
// Function to print an array of integers
void print(int a[], int numItems)
{
printf("\nThe array: ");
for(int i = 0; i < numItems; i++) {
printf("%d ", a[i]);
}
printf("\n\n");
}
Here’s the code for the animation
(downloadable here from the link above)
not that in this code, the array values are different and contain negative numbers
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h> // For Windows API functions
#else
#include <unistd.h> // For usleep
#endif
// ANSI escape codes for colors
#define RESET_COLOR "\x1b[0m"
#define RED "\x1b[31m"
#define GREEN "\x1b[32m"
#define BLUE "\x1b[34m"
#define YELLOW "\x1b[33m"
// Global sleep time definitions (in milliseconds)
#define SLEEP_TIME_MS 1000
int findMaxAbsValue(int a[], int numItems) {
int maxAbsValue = abs(a[0]);
for (int i = 1; i < numItems; i++) {
if (abs(a[i]) > maxAbsValue) {
maxAbsValue = abs(a[i]);
}
}
return maxAbsValue;
}
void printArrayAsColumns(int a[], int numItems, int maxAbsValue, int compareIndex, int swapped, int pass, int sortedIndex) {
printf("Pass number: %d\n", pass); // Print the pass number at the top
for (int level = maxAbsValue; level > 0; level--) {
for (int i = 0; i < numItems; i++) {
if (i >= sortedIndex) {
printf(YELLOW);
} else if (i == compareIndex || i == compareIndex + 1) {
printf(swapped ? (i == compareIndex ? RED : BLUE) : GREEN);
}
if (a[i] >= level) {
printf("| ");
} else {
printf(" ");
}
printf(RESET_COLOR);
}
printf("\n");
}
for (int i = 0; i < numItems; i++) {
printf("--");
}
printf("\n");
for (int level = 1; level <= maxAbsValue; level++) {
for (int i = 0; i < numItems; i++) {
if (i >= sortedIndex) {
printf(YELLOW);
} else if (i == compareIndex || i == compareIndex + 1) {
printf(swapped ? (i == compareIndex ? RED : BLUE) : GREEN);
}
if (a[i] <= -level) {
printf("| ");
} else {
printf(" ");
}
printf(RESET_COLOR);
}
printf("\n");
}
for (int i = 0; i < numItems; i++) {
if (i >= sortedIndex) {
printf(YELLOW);
} else if (i == compareIndex || i == compareIndex + 1) {
printf(swapped ? (i == compareIndex ? RED : BLUE) : GREEN);
}
printf("%d ", a[i]);
printf(RESET_COLOR);
}
printf("\n");
}
void animateBubbleSort(int a[], int numItems, int stepThrough) {
int maxAbsValue = findMaxAbsValue(a, numItems);
int i, j, temp, pass = 0;
for (i = 0; i < numItems - 1; i++) {
pass++;
for (j = 0; j < numItems - i - 1; j++) {
int swapped = 0;
// Clear the screen
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
printArrayAsColumns(a, numItems, maxAbsValue, j, swapped, pass, numItems - i);
// Print the initial comparison statement
printf("Now comparing %d and %d.\n", a[j], a[j + 1]);
if (a[j] > a[j + 1]) {
printf("%d is greater than %d, will swap.\n", a[j], a[j + 1]);
swapped = 1;
if (stepThrough) {
printf("Press enter to continue to the next step...\n");
while (getchar() != '\n'); // Wait for user to press enter
} else {
#ifdef _WIN32
Sleep(SLEEP_TIME_MS); // Sleep for Windows
#else
usleep(SLEEP_TIME_MS * 1000); // Sleep for Unix, converting milliseconds to microseconds
#endif
}
// Perform the swap
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
// Clear the screen for swap update
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
printArrayAsColumns(a, numItems, maxAbsValue, j, swapped, pass, numItems - i);
// Print the swap confirmation
printf("Swapped: %d (red) and %d (blue).\n", a[j + 1], a[j]);
} else {
// Print the no swap statement
printf("No swap needed: %d is not greater than %d.\n", a[j], a[j + 1]);
}
if (stepThrough) {
printf("Press enter to continue to the next step...\n");
while (getchar() != '\n'); // Wait for user to press enter
} else {
#ifdef _WIN32
Sleep(SLEEP_TIME_MS); // Sleep for Windows
#else
usleep(SLEEP_TIME_MS * 1000); // Sleep for Unix, converting milliseconds to microseconds
#endif
}
}
}
}
int main() {
int myArray[] = {4, -3, 1, -2, 5};
int numItems = sizeof(myArray) / sizeof(myArray[0]);
char userChoice;
printf("Do you want to step through the animation (s) or run it in full (f)? [s/f]: ");
scanf(" %c", &userChoice); // Note the space before %c to consume any whitespace characters
// Consume the newline character left in the input buffer by scanf
while (getchar() != '\n');
// Call the animateBubbleSort function
if (userChoice == 's' || userChoice == 'S') {
animateBubbleSort(myArray, numItems, 1); // Step through
} else {
animateBubbleSort(myArray, numItems, 0); // Run in full
}
return 0;
}
© 2023 Vedesh Kungebeharry. All rights reserved.


