push_swap/sources/sort_radix.c
2024-04-20 14:48:16 +02:00

62 lines
1.6 KiB
C
Executable file

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_radix.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/13 12:07:02 by rparodi #+# #+# */
/* Updated: 2024/03/06 11:55:32 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/push_swap.h"
static int get_max_bits(t_list **stack)
{
t_list *head;
int max;
int max_bits;
head = *stack;
max = head->index;
max_bits = 0;
while (head)
{
if (head->index > max)
max = head->index;
head = head->next;
}
while ((max >> max_bits) != 0)
max_bits++;
return (max_bits);
}
void radix_sort(t_list **stack_a, t_list **stack_b)
{
t_list *head_a;
int i;
int j;
int size;
int max_bits;
i = 0;
head_a = *stack_a;
size = ft_lstsize(head_a);
max_bits = get_max_bits(stack_a);
while (i < max_bits)
{
j = 0;
while (j++ < size)
{
head_a = *stack_a;
if (((head_a->index >> i) & 1) == 1)
ra(stack_a);
else
pb(stack_a, stack_b);
}
while (ft_lstsize(*stack_b) != 0)
pa(stack_a, stack_b);
i++;
}
}