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-01 11:15:17 -05:00
|
|
|
/* Updated: 2022/12/01 17:00:04 by cchauvet ### ########.fr */
|
2022-11-23 15:44:27 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "pushswap.h"
|
|
|
|
|
2022-12-01 11:15:17 -05:00
|
|
|
static unsigned int *ft_values_simplifier(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
|
|
|
|
|
|
|
out = malloc(sizeof(int) * (size + 1));
|
|
|
|
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);
|
|
|
|
out[max_index] = (size - i);
|
|
|
|
tab_in[max_index] = INT_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-01 11:15:17 -05:00
|
|
|
static unsigned int *ft_tab_init(int *tab_in, int size, unsigned **tab_a, unsigned **tab_b)
|
2022-11-23 15:44:27 -05:00
|
|
|
{
|
2022-12-01 11:15:17 -05:00
|
|
|
*tab_b = malloc(sizeof(int) * (size + 1));
|
|
|
|
*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-01 11:15:17 -05:00
|
|
|
void ft_sort(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);
|
|
|
|
}
|