sort 5: needs rotate optimalisation

This commit is contained in:
whaffman 2024-11-07 19:14:56 +01:00
parent bb8ea578a6
commit 4e53098f21
5 changed files with 12 additions and 7 deletions

2
.vscode/launch.json vendored
View File

@ -6,7 +6,7 @@
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/push_swap",
"args": ["2", "5", "4", "1"],
"args": ["20", "86", "63", "13", "84"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],

View File

@ -21,6 +21,7 @@ typedef struct s_state
t_list *b;
} t_state;
int rotate_a_to_top(t_state *state);
int new_element(t_list **stack, const char *str);
t_list *ft_lstat(t_list *list, int n);
void print_stack(const char *name, t_list *stack);

View File

@ -16,13 +16,13 @@
void pb(t_state *state)
{
push(&(state->a), &(state->b));
ft_printf("pa\n");
ft_printf("pb\n");
}
void pa(t_state *state)
{
push(&(state->b), &(state->a));
ft_printf("pb\n");
ft_printf("pa\n");
}
void push(t_list **stack_1, t_list **stack_2)

View File

@ -22,7 +22,7 @@ void sa(t_state *state)
void sb(t_state *state)
{
swap(&(state->b));
ft_printf("rb\n");
ft_printf("sb\n");
}
void ss(t_state *state)

View File

@ -27,16 +27,20 @@ int merge_to_a(t_state *state)
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);
rra(state);
ra(state);
return (0);
}
while (*((int *) state->b->content) > *((int *) state->a->content) && n > 0)
{
rra(state);
ra(state);
n--;
}
pa(state);