aboutsummaryrefslogtreecommitdiff
path: root/src/algo/ft_mergesort.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/algo/ft_mergesort.c')
-rw-r--r--src/algo/ft_mergesort.c24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/algo/ft_mergesort.c b/src/algo/ft_mergesort.c
index 25b4255..8556145 100644
--- a/src/algo/ft_mergesort.c
+++ b/src/algo/ft_mergesort.c
@@ -6,12 +6,19 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/10 02:26:41 by cacharle #+# #+# */
-/* Updated: 2020/02/13 23:14:21 by cacharle ### ########.fr */
+/* Updated: 2020/04/04 22:40:55 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft_algo.h"
+/*
+** \brief Helper function to return in case of error
+** \param left Left subarray
+** \param right Right subarray
+** \return -1 to indicate error
+*/
+
static int st_mergesort_error(void *left, void *right)
{
free(left);
@@ -19,6 +26,13 @@ static int st_mergesort_error(void *left, void *right)
return (-1);
}
+/*
+** \brief Merge 2 sorted arrays
+** \param arrays Struct containing the arrays (base, left, right)
+** \param nel Number of element in base
+** \param compar Comparison function
+*/
+
static void st_merge_sorted(struct s_merge_sorted_arrays *arrays, size_t nel,
size_t width, int (*compar)(const void *, const void *))
{
@@ -49,6 +63,14 @@ static void st_merge_sorted(struct s_merge_sorted_arrays *arrays, size_t nel,
arrays->right + ri++ * width, width);
}
+/*
+** \brief Sort an array using the merge sort algorithm
+** \param base Array to sort
+** \param nel Number of element in the array
+** \param width Size of each element
+** \return 0 on success, -1 on error
+*/
+
int ft_mergesort(void *base, size_t nel, size_t width,
int (*compar)(const void *, const void *))
{