42_push_swap/ft_sort.c
2022-12-01 17:15:17 +01:00

66 lines
1.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */
/* Updated: 2022/12/01 17:00:04 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
static unsigned int *ft_values_simplifier(int *tab_in, int size)
{
int i;
unsigned int *out;
int max_index;
out = malloc(sizeof(int) * (size + 1));
if (out == NULL)
{
free(tab_in);
return (NULL);
}
i = 0;
while (i < size)
{
max_index = ft_get_max_index(tab_in, size);
out[max_index] = (size - i);
tab_in[max_index] = INT_MIN;
i++;
}
out[size] = STOP_VALUE;
free (tab_in);
return (out);
}
static unsigned int *ft_tab_init(int *tab_in, int size, unsigned **tab_a, unsigned **tab_b)
{
*tab_b = malloc(sizeof(int) * (size + 1));
*tab_a = ft_values_simplifier(tab_in, size);
if (tab_a == NULL || tab_b == NULL)
{
free(tab_in);
free(tab_a);
free(tab_b);
return (NULL);
}
*(tab_b)[0] = STOP_VALUE;
return (*tab_a);
}
void ft_sort(int *tab_in, int size_a)
{
unsigned int *tab_a;
unsigned int *tab_b;
tab_b = NULL;
tab_a = NULL;
if (ft_tab_init(tab_in, size_a, &tab_a, &tab_b) == NULL)
return ;
ft_radix_sort(tab_a, tab_b);
}