aboutsummaryrefslogtreecommitdiff
path: root/src/algo/ft_qsort.c
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-04-04 23:36:19 +0200
committerCharles <sircharlesaze@gmail.com>2020-04-04 23:36:19 +0200
commit65cf641e9533b190db870d0cc46f2f852239ebf6 (patch)
treedf2002170b96d189493e54cb484390fb2af2a2d1 /src/algo/ft_qsort.c
parent42316d8393d32bd88fb8e0cba6825185f78dacd0 (diff)
downloadlibft-65cf641e9533b190db870d0cc46f2f852239ebf6.tar.gz
libft-65cf641e9533b190db870d0cc46f2f852239ebf6.tar.bz2
libft-65cf641e9533b190db870d0cc46f2f852239ebf6.zip
Added test for ft_strsjoin, ft_strsjoinf, ft_vecpush_safe, Added doc for algo functions, tested functions
Diffstat (limited to 'src/algo/ft_qsort.c')
-rw-r--r--src/algo/ft_qsort.c35
1 files changed, 34 insertions, 1 deletions
diff --git a/src/algo/ft_qsort.c b/src/algo/ft_qsort.c
index 9bcfcdf..0c1c0f7 100644
--- a/src/algo/ft_qsort.c
+++ b/src/algo/ft_qsort.c
@@ -6,12 +6,19 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/19 07:25:51 by cacharle #+# #+# */
-/* Updated: 2020/02/10 02:55:14 by cacharle ### ########.fr */
+/* Updated: 2020/04/04 22:10:29 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft_algo.h"
+/*
+** \brief Helper to create a new range
+** \param lo Lower bound
+** \param hi Higher bound
+** \return Range struct (not allocated)
+*/
+
static t_ftrange ft_range_new(int lo, int hi)
{
t_ftrange range;
@@ -21,6 +28,16 @@ static t_ftrange ft_range_new(int lo, int hi)
return (range);
}
+/*
+** \brief Array partitionning,
+** takes a pivot and place every element lower than it before
+** and every element greater after
+** \param base Partitioned array
+** \param range Lower and upper bound
+** \param width Single element size
+** \param compar Comparison function
+*/
+
static int ft_qsort_partition(void *base, t_ftrange range,
size_t width, t_ftcompar_func compar)
{
@@ -43,6 +60,14 @@ static int ft_qsort_partition(void *base, t_ftrange range,
return (p);
}
+/*
+** \brief Qsort recursion function
+** \param base Array to sort
+** \param range Lower and upper bound which define the area to sort
+** \param width Single element size
+** \param compar Comparision function
+*/
+
static void ft_qsort_rec(void *base, t_ftrange range,
size_t width, t_ftcompar_func compar)
{
@@ -55,6 +80,14 @@ static void ft_qsort_rec(void *base, t_ftrange range,
ft_qsort_rec(base, ft_range_new(pivot + 1, range.hi), width, compar);
}
+/*
+** \brief Sort an array using the Quick sort algorithm
+** \param base Array to sort
+** \param nel Number of element in the array
+** \param width Size of each element
+** \param compar Comparision function
+*/
+
void ft_qsort(void *base, size_t nel, size_t width,
t_ftcompar_func compar)
{