From b7ba0b39e160329d9f17bd3add261d7454fd5ce7 Mon Sep 17 00:00:00 2001 From: MarcUs7i Date: Wed, 7 May 2025 18:20:42 +0200 Subject: [PATCH] Completed bubble sort --- bubble_sort.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ bubble_sort.h | 4 ++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/bubble_sort.c b/bubble_sort.c index 969eae4..3eb1398 100644 --- a/bubble_sort.c +++ b/bubble_sort.c @@ -12,3 +12,51 @@ #include "bubble_sort.h" +/** + * Sorts the given list according to the bubble sort strategy. + * + * @param list The list to be sorted. + * @param criterion The pointer to the function that implements the sorting criterion. + * That function accepts two integer parameters and returns a boolean value. + */ +void bubble_sort_list(IntList list, criterion_fn criterion) { + if (!list_is_valid(list) || criterion == 0) { + return; + } + + int size = list_get_size(list); + if (size <= 1) { + return; + } + + for (int i = 0; i < size - 1; i++) { + int swapped = 0; + + for (int j = 0; j < size - i - 1; j++) { + int current = list_get_at(list, j); + int next = list_get_at(list, j + 1); + + if (!criterion(current, next)) { + list_swap(list, j, j + 1); + swapped = 1; + } + } + + if (!swapped) { + break; + } + } +} + +/** + * Sorts the given array according to the bubble sort strategy. + * + * @param array The array to be sorted. + * @param length The length of the array. + * @param criterion The pointer to the function that implements the sorting criterion. + * That function accepts two integer parameters and returns a boolean value. + */ +void bubble_sort_array(int array[], int length, criterion_fn criterion) { + +} + diff --git a/bubble_sort.h b/bubble_sort.h index d4443e0..f7480e3 100644 --- a/bubble_sort.h +++ b/bubble_sort.h @@ -24,7 +24,7 @@ * @param criterion The pointer to the function that implements the sorting criterion. * That function accepts two integer parameters and returns a boolean value. */ -void bubble_sort_list(); +void bubble_sort_list(IntList list, criterion_fn criterion); /** * Sorts the given array according to the bubble sort strategy. @@ -34,6 +34,6 @@ void bubble_sort_list(); * @param criterion The pointer to the function that implements the sorting criterion. * That function accepts two integer parameters and returns a boolean value. */ -void bubble_sort_array(); +void bubble_sort_array(int array[], int length, criterion_fn criterion); #endif