42_push_swap/ft_sort.c

68 lines
1.9 KiB
C
Raw Normal View History

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