2022-11-23 15:44:27 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* ft_radix.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2022/11/15 18:16:31 by cchauvet #+# #+# */
|
2022-11-30 14:11:35 -05:00
|
|
|
/* Updated: 2022/11/30 20:06:39 by cchauvet ### ########.fr */
|
2022-11-23 15:44:27 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "pushswap.h"
|
|
|
|
|
|
|
|
void ft_radix_sort(int *tab_a, int *tab_b)
|
|
|
|
{
|
2022-11-30 14:11:35 -05:00
|
|
|
int tab_index;
|
|
|
|
int loop_index;
|
|
|
|
int shift;
|
2022-11-23 15:44:27 -05:00
|
|
|
|
2022-11-30 07:21:32 -05:00
|
|
|
while (!ft_is_sorted(tab_a))
|
2022-11-23 15:44:27 -05:00
|
|
|
{
|
2022-11-30 14:11:35 -05:00
|
|
|
tab_index = ft_tablen(tab_a) - 1;
|
|
|
|
loop_index = tab_index;
|
|
|
|
shift = ft_bit_finder_right(ft_get_bit_min(tab_a), 1);
|
|
|
|
while (loop_index > 0 && !ft_is_sorted(tab_a))
|
2022-11-23 15:44:27 -05:00
|
|
|
{
|
2022-11-30 14:11:35 -05:00
|
|
|
if (tab_a[tab_index] >> shift & 1)
|
|
|
|
{
|
2022-11-30 07:21:32 -05:00
|
|
|
ft_pb(tab_a, tab_b);
|
2022-11-30 14:11:35 -05:00
|
|
|
tab_index--;
|
|
|
|
}
|
2022-11-23 15:44:27 -05:00
|
|
|
else
|
2022-11-30 07:21:32 -05:00
|
|
|
ft_ra(tab_a);
|
2022-11-30 14:11:35 -05:00
|
|
|
loop_index--;
|
2022-11-23 15:44:27 -05:00
|
|
|
}
|
2022-11-30 07:21:32 -05:00
|
|
|
while (tab_b[0] != -1)
|
|
|
|
ft_pa(tab_a, tab_b);
|
2022-11-30 14:11:35 -05:00
|
|
|
tab_index = ft_tablen(tab_a);
|
|
|
|
while (--tab_index >= 0 && !ft_is_sorted(tab_a))
|
|
|
|
tab_a[tab_index] = tab_a[tab_index] << 1;
|
2022-11-23 15:44:27 -05:00
|
|
|
}
|
2022-11-30 14:11:35 -05:00
|
|
|
free(tab_a);
|
|
|
|
free(tab_b);
|
2022-11-23 15:44:27 -05:00
|
|
|
}
|