/* CS107
 * Lecture Example: generics
 *
 * This file shows how to implement a generic
 * bubble sort function that uses void * pointers
 * and function pointers.
 */
#include <stdio.h>
#include <string.h>
#include "bubble_sort.h"

/* Example 1: sorting ints */

int sort_ints_ascending(void *ptr1, void *ptr2) {
    int actualFirstElem = *(int *)ptr1;
    int actualSecondElem = *(int *)ptr2;
    return actualFirstElem - actualSecondElem;
}

void test_sort_ints() {
    int nums[] = {4, 2, 12, -5, 56, 14};
    int nums_count = sizeof(nums) / sizeof(nums[0]);

    printf("Sorting the following integers in ascending order: [");
    for (int i = 0; i < nums_count; i++) {
        printf("%d, ", nums[i]);
    }
    printf("]\n");
    
    bubble_sort(nums, nums_count, sizeof(nums[0]), sort_ints_ascending);

    printf("Sorted: [");
    for (int i = 0; i < nums_count; i++) {
        printf("%d, ", nums[i]);
    }
    printf("]\n");
}

/* Example 2: sorting strings */

void test_sort_strings() {
    char *strs[] = {
                    "hello",
                    "cs107",
                    "howdy",
                    "pointers",
                    "void *"
    };
    int strs_count = sizeof(strs) / sizeof(strs[0]);

    printf("Sorting the following strings in alphabetical order: [");
    for (int i = 0; i < strs_count; i++) {
        printf("\"%s\", ", strs[i]);
    }
    printf("]\n");

    // TODO: use bubble_sort to sort strs in alphabetical order
    
    printf("Sorted: [");
    for (int i = 0; i < strs_count; i++) {
        printf("\"%s\", ", strs[i]);
    }
    printf("]\n");
}


int main(int argc, char *argv[]) {
    test_sort_ints();
    test_sort_strings();
    return 0;
}
