sort5
This commit is contained in:
parent
e160b4ed63
commit
2c3769be68
@ -21,8 +21,11 @@ typedef struct s_state
|
||||
t_list *b;
|
||||
} t_state;
|
||||
|
||||
void rotate_a_to(int index, t_state *state);
|
||||
void rotate_b_to(int index, t_state *state);
|
||||
int index_of(int number, t_list *stack);
|
||||
int put_at_index(int number, t_list *stack);
|
||||
int put_at_index_asc(int number, t_list *stack);
|
||||
int put_at_index_desc(int number, t_list *stack);
|
||||
int rotate_a_to_top(t_state *state);
|
||||
int shortest_rotate(int index, int size);
|
||||
int new_element(t_list **stack, const char *str);
|
||||
|
||||
@ -16,33 +16,15 @@
|
||||
|
||||
int rotate_a_to_top(t_state *state)
|
||||
{
|
||||
while (*(int *) state->a->content != stack_min(state->a))
|
||||
ra(state);
|
||||
rotate_a_to(index_of(stack_min(state->a), state->a), state);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int merge_to_a(t_state *state)
|
||||
{
|
||||
int n;
|
||||
const int b = *(int *) state->b->content;
|
||||
|
||||
n = ft_lstsize(state->a);
|
||||
if (*(int *) state->b->content < stack_min(state->a))
|
||||
{
|
||||
pa(state);
|
||||
return (0);
|
||||
}
|
||||
else if (*(int *) state->b->content > stack_max(state->a))
|
||||
{
|
||||
rotate_a_to_top(state);
|
||||
pa(state);
|
||||
ra(state);
|
||||
return (0);
|
||||
}
|
||||
while (*((int *) state->b->content) > *((int *) state->a->content) && n > 0)
|
||||
{
|
||||
ra(state);
|
||||
n--;
|
||||
}
|
||||
rotate_a_to(put_at_index_asc(b, state->a), state);
|
||||
pa(state);
|
||||
return (1);
|
||||
}
|
||||
@ -51,12 +33,12 @@ int sort5(t_state *state)
|
||||
{
|
||||
pb(state);
|
||||
pb(state);
|
||||
if (*(int *)state->b->content < *(int *)state->b->next->content)
|
||||
if (*(int *)state->b->content > *(int *)state->b->next->content)
|
||||
sb(state);
|
||||
sort3(state);
|
||||
merge_to_a(state);
|
||||
merge_to_a(state);
|
||||
while (*(int *) state->a->content > *(int *) ft_lstlast(state->a)->content)
|
||||
rra(state);
|
||||
rotate_a_to_top(state);
|
||||
|
||||
return (1);
|
||||
}
|
||||
|
||||
@ -20,11 +20,36 @@ int shortest_rotate(int index, int size)
|
||||
}
|
||||
|
||||
//TODO
|
||||
void rotate_to(int index, t_state state, char c)
|
||||
void rotate_a_to(int index, t_state *state)
|
||||
{
|
||||
if (c == 'a')
|
||||
int n;
|
||||
|
||||
n = shortest_rotate(index, ft_lstsize(state->a));
|
||||
while (n > 0)
|
||||
{
|
||||
return ;
|
||||
ra(state);
|
||||
n--;
|
||||
}
|
||||
while (n < 0)
|
||||
{
|
||||
rra(state);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
void rotate_b_to(int index, t_state *state)
|
||||
{
|
||||
int n;
|
||||
|
||||
n = shortest_rotate(index, ft_lstsize(state->b));
|
||||
while (n > 0)
|
||||
{
|
||||
rb(state);
|
||||
n--;
|
||||
}
|
||||
while (n < 0)
|
||||
{
|
||||
rrb(state);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,11 +63,45 @@ int index_of(int number, t_list *stack)
|
||||
if (*(int *) stack->content == number)
|
||||
return (i);
|
||||
stack = stack->next;
|
||||
i++;
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
int put_at_index(int number, t_list *stack)
|
||||
int ft_min(int a, int b)
|
||||
{
|
||||
if (a < b)
|
||||
return (a);
|
||||
return (b);
|
||||
}
|
||||
|
||||
int put_at_index_desc(int number, t_list *stack)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (number < stack_min(stack) || number > stack_max(stack))
|
||||
i = ft_min(
|
||||
index_of(stack_min(stack), stack),
|
||||
index_of(stack_max(stack), stack));
|
||||
else
|
||||
{
|
||||
while (number < *(int *) ft_lstat(stack, i)->content)
|
||||
i++;
|
||||
while (number < *(int *) ft_lstat(stack, i)->content
|
||||
&& number < *(int *) ft_lstat(stack, i + 1)->content
|
||||
&& i < ft_lstsize(stack))
|
||||
{
|
||||
i++;
|
||||
}
|
||||
ft_printf("%d\n", i);
|
||||
}
|
||||
if (i == ft_lstsize(stack))
|
||||
return (0);
|
||||
return (i);
|
||||
}
|
||||
|
||||
int put_at_index_asc(int number, t_list *stack)
|
||||
{
|
||||
int i;
|
||||
|
||||
@ -51,12 +110,15 @@ int put_at_index(int number, t_list *stack)
|
||||
i = index_of(stack_min(stack), stack);
|
||||
else
|
||||
{
|
||||
while (number > *(int *) ft_lstat(stack, i)->content
|
||||
&& number > *(int *) ft_lstat(stack, i)->content
|
||||
&& i < ft_lstsize(stack))
|
||||
while (number < *(int *) ft_lstat(stack, i)->content)
|
||||
i++;
|
||||
while (i + 1 < ft_lstsize(stack)
|
||||
&&number > *(int *) ft_lstat(stack, i)->content
|
||||
&& number > *(int *) ft_lstat(stack, i + 1)->content)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (i == ft_lstsize(stack))
|
||||
return (0);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user