From f9280842c1a57109941582b3ee48a3b9b05d67b5 Mon Sep 17 00:00:00 2001 From: MarcUs7i Date: Wed, 7 May 2025 18:21:03 +0200 Subject: [PATCH] Completed insertion sort --- insertion_sort.c | 42 +++++++++++++++++++++++++++++++++++++++++- insertion_sort.h | 4 ++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/insertion_sort.c b/insertion_sort.c index 7787de1..1c3a8b1 100644 --- a/insertion_sort.c +++ b/insertion_sort.c @@ -10,4 +10,44 @@ *----------------------------------------------------------------------------- */ -#include "insertion_sort.h" \ No newline at end of file +#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) { + +} \ No newline at end of file diff --git a/insertion_sort.h b/insertion_sort.h index 4ab6a62..bcff586 100644 --- a/insertion_sort.h +++ b/insertion_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 insertion_sort_list(); +void insertion_sort_list(IntList list, criterion_fn criterion); /** * Sorts the given array according to the insertion sort strategy. @@ -34,6 +34,6 @@ void insertion_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 insertion_sort_array(); +void insertion_sort_array(int array[], int length, criterion_fn criterion); #endif