aboutsummaryrefslogtreecommitdiff
path: root/src/algo/ft_qsort.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-08-02 11:05:33 +0200
commit5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde (patch)
tree80911dc3c32e9f230750e7e1042d413dfb6efab2 /src/algo/ft_qsort.c
parentee32953ea79616e72f5428cdf40c834714a891c9 (diff)
parentb96b82194ccad2cddbb46b77aa1962a57c47ff44 (diff)
downloadlibft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.gz
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.tar.bz2
libft-5d2f925b20ceaea4122c59d2d2c4e7d4ae991fde.zip
Merge branch 'master' into ft_ssl
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)
{