53 lines
No EOL
1.7 KiB
C
53 lines
No EOL
1.7 KiB
C
/*-----------------------------------------------------------------------------
|
|
* HTBLA-Leonding
|
|
*-----------------------------------------------------------------------------
|
|
* Exercise Number: S05
|
|
* Title: Insertion sort implementation
|
|
* Author: Marc Tismonar
|
|
*-----------------------------------------------------------------------------
|
|
* Description:
|
|
* Implements the insertion sort strategy
|
|
*-----------------------------------------------------------------------------
|
|
*/
|
|
|
|
#include "insertion_sort.h"
|
|
|
|
/**
|
|
* Sorts the given list according to the insertion 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 insertion_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 = 1; i < size; i++) {
|
|
int current = list_get_at(list, i);
|
|
int j = i - 1;
|
|
|
|
while (j >= 0 && !criterion(list_get_at(list, j), current)) {
|
|
list_swap(list, j, j + 1);
|
|
j--;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sorts the given array according to the insertion 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 insertion_sort_array(int array[], int length, criterion_fn criterion) {
|
|
|
|
} |