-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix_sort.c
70 lines (63 loc) · 1.74 KB
/
radix_sort.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
63
64
65
66
67
68
69
70
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* radix_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lstorey <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 10:40:36 by lstorey #+# #+# */
/* Updated: 2024/04/04 12:46:10 by lstorey ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int list_length(t_ps_list **stack)
{
t_ps_list *tmp;
int i;
i = 0;
tmp = (*stack);
if (tmp == NULL)
return (0);
while (tmp)
{
tmp = tmp->next;
i++;
}
return (i);
}
static int find_max_bits(t_ps_list **stack_a)
{
int max;
int max_bits;
max_bits = 0;
max = list_length(stack_a);
while (max >> max_bits != 0)
max_bits++;
return (max_bits);
}
void radix_sort(t_ps_list **stack_a, t_ps_list **stack_b)
{
int i;
static int j = -1;
int bits;
int size_of_list;
t_ps_list *tmp;
tmp = (*stack_a);
bits = find_max_bits(stack_a);
size_of_list = list_length(stack_a);
while (++j < bits || sorted(stack_a))
{
i = 0;
while (i++ < size_of_list && stack_a)
{
if ((((*stack_a)->index >> j) & 1) == 1)
ra(stack_a);
else
pb(stack_a, stack_b);
}
while (list_length(stack_b) != 0)
{
pa(stack_a, stack_b);
}
}
}