42_push_swap/ft_sort.c
2022-12-11 15:49:45 +01:00

70 lines
1.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */
/* Updated: 2022/12/11 15:25:10 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
static t_tab ft_values_simplifier(long int *tab_in, int size)
{
int i;
unsigned int *out;
int max_index;
out = malloc(sizeof(unsigned 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 - 1;
tab_in[max_index] = LONG_MIN;
i++;
}
out[size] = STOP_VALUE;
return (out);
}
unsigned int *ft_tab_init(long *tab_in, int size, t_tab *tab_a, t_tab*tab_b)
{
*tab_b = malloc(sizeof(unsigned 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(long 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 ;
if (ft_tablen(tab_a) > 5)
ft_radix_sort(tab_a, tab_b);
else
ft_bozo_sort(tab_a, tab_b);
free(tab_a);
free(tab_b);
}