71 lines
1.8 KiB
C
71 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/11/28 18:08:15 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "pushswap.h"
|
|
|
|
static int *ft_values_simplifier(int *tab, int size)
|
|
{
|
|
int i;
|
|
int *out;
|
|
int max_index;
|
|
int shift;
|
|
|
|
out = malloc(sizeof(int) * (size + 1));
|
|
if (out == NULL)
|
|
{
|
|
free(tab);
|
|
return (NULL);
|
|
}
|
|
shift = ft_bit_finder(size, 1);
|
|
i = 0;
|
|
while (i < size)
|
|
{
|
|
max_index = ft_get_max_index(tab, size);
|
|
out[max_index] = (size - i) << shift;
|
|
tab[max_index] = INT_MIN;
|
|
i++;
|
|
}
|
|
out[size] = -1;
|
|
free (tab);
|
|
return (out);
|
|
}
|
|
|
|
static int *ft_tab_init(int **tab_a, int **tab_b, int size)
|
|
{
|
|
int *out_b;
|
|
int *out_a;
|
|
|
|
out_b = malloc(sizeof(int) * (size + 1));
|
|
out_a = ft_values_simplifier(*tab_a, size);
|
|
if (out_a == NULL || out_b == NULL)
|
|
{
|
|
free(*tab_a);
|
|
free(out_a);
|
|
free(out_b);
|
|
return (NULL);
|
|
}
|
|
out_b[0] = -1;
|
|
*tab_a = out_a;
|
|
*tab_b = out_b;
|
|
return (out_a);
|
|
}
|
|
|
|
void ft_sort(int *tab_a, int size_a)
|
|
{
|
|
int *tab_b;
|
|
|
|
tab_b = NULL;
|
|
if (ft_tab_init(&tab_a, &tab_b, size_a) == NULL)
|
|
return ;
|
|
ft_radix_sort(tab_a, tab_b);
|
|
}
|