62 lines
1.8 KiB
C
62 lines
1.8 KiB
C
/*-----------------------------------------------------------------------------
|
|
* HTBLA-Leonding
|
|
*-----------------------------------------------------------------------------
|
|
* Exercise Number: S05
|
|
* Title: Bubble sort implementation
|
|
* Author: Marc Tismonar
|
|
*-----------------------------------------------------------------------------
|
|
* Description:
|
|
* Implements the bubble sort strategy
|
|
*-----------------------------------------------------------------------------
|
|
*/
|
|
|
|
#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) {
|
|
|
|
}
|
|
|