Copied step 2

This commit is contained in:
MarcUs7i 2025-05-08 16:27:25 +02:00
parent da115cb5d6
commit 366effebec
11 changed files with 626 additions and 70 deletions

View file

@ -1,9 +1,9 @@
/*-----------------------------------------------------------------------------
* HTBLA-Leonding
*-----------------------------------------------------------------------------
* Exercise Number: S06
* Exercise Number: S05
* Title: Bubble sort implementation
* Author: */<your name>;/*
* Author: Marc Tismonar
*-----------------------------------------------------------------------------
* Description:
* Implements the bubble sort strategy
@ -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) {
}