Bubble Sort Implementation in C

#include <stdio.h>
#include <stdbool.h>
// Define comparison function type
typedef int (*CompareFunction)(const void *a, const void *b);
// Bubble sort function
void bubble_sort(void *base, size_t num, size_t size, CompareFunction compare) {
    bool swapped;
    for (size_t i = 0; i < num - 1; i++) {
        swapped = false;
        for (size_t j = 0; j < num - 1 - i; j++) {
            // Get pointers to the current and next elements
            void *current = (char *)base + j * size;
            void *next = (char *)base + (j + 1) * size;
            // Compare and swap
            if (compare(current, next) > 0) {
                // Swap the two elements
                char temp[size];
                memcpy(temp, current, size);
                memcpy(current, next, size);
                memcpy(next, temp, size);
                swapped = true;
            }
        }
        // If no swaps occurred, end sorting early
        if (!swapped) {
            break;
        }
    }
}
// Integer comparison function
int int_compare(const void *a, const void *b) {
    int int_a = *((int *)a);
    int int_b = *((int *)b);
    return int_a - int_b;
}
// Example structure
typedef struct {
    char name[50];
    int age;
    float score;
} Student;
// Compare student structures by age
int student_compare_by_age(const void *a, const void *b) {
    Student *student_a = (Student *)a;
    Student *student_b = (Student *)b;
    return student_a->age - student_b->age;
}
// Compare student structures by score
int student_compare_by_score(const void *a, const void *b) {
    Student *student_a = (Student *)a;
    Student *student_b = (Student *)b;
    return (student_a->score > student_b->score) - (student_a->score < student_b->score);
}
// Test function for integer sorting
void test_integer_sorting() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    size_t num = sizeof(arr) / sizeof(arr[0]);
    printf("Original integer array:\n");
    for (size_t i = 0; i < num; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
    bubble_sort(arr, num, sizeof(int), int_compare);
    printf("Sorted integer array:\n");
    for (size_t i = 0; i < num; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n\n");
}
void test_student_sorting_by_age() {
    Student students[] = {
        {"Alice", 20, 88.5},
        {"Bob", 19, 92.3},
        {"Charlie", 21, 85.7},
        {"David", 18, 95.0}
    };
    size_t num = sizeof(students) / sizeof(Student);
    printf("Original student array (sorted by age):\n");
    for (size_t i = 0; i < num; i++) {
        printf("%s, Age: %d, Score: %.1f\n", students[i].name, students[i].age, students[i].score);
    }
    bubble_sort(students, num, sizeof(Student), student_compare_by_age);
    printf("\nSorted student array by age:\n");
    for (size_t i = 0; i < num; i++) {
        printf("%s, Age: %d, Score: %.1f\n", students[i].name, students[i].age, students[i].score);
    }
    printf("\n");
}
void test_student_sorting_by_score() {
    Student students[] = {
        {"Alice", 20, 88.5},
        {"Bob", 19, 92.3},
        {"Charlie", 21, 85.7},
        {"David", 18, 95.0}
    };
    size_t num = sizeof(students) / sizeof(Student);
    printf("Original student array (sorted by score):\n");
    for (size_t i = 0; i < num; i++) {
        printf("%s, Age: %d, Score: %.1f\n", students[i].name, students[i].age, students[i].score);
    }
    bubble_sort(students, num, sizeof(Student), student_compare_by_score);
    printf("\nSorted student array by score:\n");
    for (size_t i = 0; i < num; i++) {
        printf("%s, Age: %d, Score: %.1f\n", students[i].name, students[i].age, students[i].score);
    }
    printf("\n");
}
int main() {
    test_integer_sorting();
    test_student_sorting_by_age();
    test_student_sorting_by_score();
    return 0;
}

Leave a Comment