Completed bubble sort
This commit is contained in:
parent
ed9381d1cd
commit
b7ba0b39e1
2 changed files with 50 additions and 2 deletions
|
|
@ -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) {
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue