-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_swap_radix.c
62 lines (57 loc) · 1.63 KB
/
push_swap_radix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap_radix.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hdescamp <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/16 13:22:00 by hdescamp #+# #+# */
/* Updated: 2024/12/17 10:47:06 by hdescamp ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int get_max_bits(t_tlist **stack)
{
t_tlist *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_tlist **stack_a, t_tlist **stack_b)
{
t_tlist *head_a;
int i;
int j;
int size;
int max_bits;
i = 0;
head_a = *stack_a;
size = ft_lsttsize(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_lsttsize(*stack_b) != 0)
pa(stack_a, stack_b);
i++;
}
}