64 lines
1.7 KiB
C
64 lines
1.7 KiB
C
|
/* ************************************************************************** */
|
||
|
/* */
|
||
|
/* ::: :::::::: */
|
||
|
/* ft_sort.c :+: :+: :+: */
|
||
|
/* +:+ +:+ +:+ */
|
||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||
|
/* +#+#+#+#+#+ +#+ */
|
||
|
/* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */
|
||
|
/* Updated: 2022/11/23 18:47:11 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, i);
|
||
|
out[max_index] = (size - i) << shift;
|
||
|
tab[max_index] = INT_MIN;
|
||
|
i++;
|
||
|
}
|
||
|
free (tab);
|
||
|
return (out);
|
||
|
}
|
||
|
|
||
|
static int *ft_tab_init(int *tab_a, int *tab_b, int size)
|
||
|
{
|
||
|
size++;
|
||
|
tab_b = malloc(sizeof(int) * (size + 1));
|
||
|
tab_a = ft_values_simplifier(tab_a, size);
|
||
|
if (tab_a == NULL || tab_b == NULL)
|
||
|
{
|
||
|
free(tab_a);
|
||
|
free(tab_b);
|
||
|
return (NULL);
|
||
|
}
|
||
|
tab_b[size] = -1;
|
||
|
return (tab_a);
|
||
|
}
|
||
|
|
||
|
void ft_sort(int *tab_a, int size_a)
|
||
|
{
|
||
|
int *tab_b;
|
||
|
|
||
|
if (ft_tab_init(tab_a, tab_b, size_a) == NULL)
|
||
|
return ;
|
||
|
ft_radix_sort(tab_a, tab_b);
|
||
|
}
|