-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort1.c
90 lines (82 loc) · 2.16 KB
/
sort1.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
82
83
84
85
86
87
88
89
90
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: raqferre <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 11:32:59 by raqferre #+# #+# */
/* Updated: 2023/03/31 17:26:08 by raqferre ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int get_highest_index(t_list **stack_a)
{
int temp;
t_list *aux;
aux = *stack_a;
temp = aux -> index;
while (aux)
{
if (aux -> index > temp)
temp = aux -> index;
aux = aux -> next;
}
return (temp);
}
int bits(t_list **stack_a)
{
int max_index;
int max_bits;
max_bits = 0;
max_index = list_size(stack_a) - 1;
while (max_index != 0)
{
max_index = max_index / 2;
max_bits++;
}
return (max_bits);
}
void radix(t_list **stack_a, t_list **stack_b)
{
t_list *aux;
int i;
int j;
int size;
int max_bits;
i = 0;
aux = *stack_a;
size = list_size(stack_a);
max_bits = bits(stack_a);
while (i < max_bits)
{
j = 0;
while (j++ < size)
{
aux = *stack_a;
if (((aux->index >> i) & 1) == 1)
ra(stack_a, 1);
else
pb(stack_a, stack_b, 1);
}
while (list_size(stack_b) != 0)
pa(stack_a, stack_b, 1);
i++;
}
}
void sort_5(t_list **stack_a, t_list **stack_b)
{
if ((*stack_a)-> next -> next -> next -> next -> index == 0)
rra(stack_a, 1);
while ((*stack_a)->index != 0)
ra(stack_a, 1);
pb(stack_a, stack_b, 1);
while ((*stack_a)-> index != 1)
ra(stack_a, 1);
pb(stack_a, stack_b, 1);
sort_3(stack_a);
if ((*stack_b)-> index < (*stack_b)-> next -> index)
sb(*stack_b, 1);
pa(stack_a, stack_b, 1);
pa(stack_a, stack_b, 1);
}