-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix.c
81 lines (75 loc) · 1.92 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* radix.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fatkeski <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/20 15:15:06 by fatkeski #+# #+# */
/* Updated: 2024/03/20 15:31:51 by fatkeski ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void assign_flag(t_list **stack)
{
int min;
int stack_len;
int flag_num;
t_list *temp;
t_list *min_node;
flag_num = 0;
stack_len = ft_lstsize(*stack);
while (flag_num < stack_len)
{
temp = *stack;
min = INT_MAX;
while (temp != 0)
{
if (((temp->content) <= min) && (temp->flag == -1))
{
min = temp->content;
min_node = temp;
}
temp = temp->next;
}
min_node->flag = flag_num;
flag_num++;
}
}
int is_sorted(t_list *stack, int min_flag)
{
while (stack != 0)
{
if (min_flag == stack->flag)
{
stack = stack->next;
min_flag++;
}
else
return (0);
}
return (1);
}
void radix_sort(t_list **stack_a, t_list **stack_b)
{
int i;
int j;
int stack_a_len;
i = 0;
stack_a_len = ft_lstsize(*stack_a);
while (i < stack_a_len && is_sorted(*stack_a, 0) == 0)
{
j = 0;
while (j < stack_a_len)
{
if (((((*stack_a)->flag) >> i) & 1) == 0)
push(stack_b, stack_a, 'b');
else
rotate(stack_a, 'a');
j++;
}
while (*stack_b != 0)
push(stack_a, stack_b, 'a');
i++;
}
}