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", "type": "cppdbg",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/push_swap", "program": "${workspaceFolder}/push_swap",
"args": ["2", "5", "4", "1"], "args": ["20", "86", "63", "13", "84"],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"environment": [], "environment": [],

View File

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

View File

@ -16,13 +16,13 @@
void pb(t_state *state) void pb(t_state *state)
{ {
push(&(state->a), &(state->b)); push(&(state->a), &(state->b));
ft_printf("pa\n"); ft_printf("pb\n");
} }
void pa(t_state *state) void pa(t_state *state)
{ {
push(&(state->b), &(state->a)); push(&(state->b), &(state->a));
ft_printf("pb\n"); ft_printf("pa\n");
} }
void push(t_list **stack_1, t_list **stack_2) 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) void sb(t_state *state)
{ {
swap(&(state->b)); swap(&(state->b));
ft_printf("rb\n"); ft_printf("sb\n");
} }
void ss(t_state *state) void ss(t_state *state)

View File

@ -23,20 +23,24 @@ int rotate_a_to_top(t_state *state)
int merge_to_a(t_state *state) int merge_to_a(t_state *state)
{ {
int n; int n;
n = ft_lstsize(state->a); n = ft_lstsize(state->a);
if (*(int *) state->b->content < stack_min(state->a)) if (*(int *) state->b->content < stack_min(state->a))
{
pa(state); pa(state);
return (0);
}
else if (*(int *) state->b->content > stack_max(state->a)) else if (*(int *) state->b->content > stack_max(state->a))
{ {
rotate_a_to_top(state); rotate_a_to_top(state);
pa(state); pa(state);
rra(state); ra(state);
return (0);
} }
while (*((int *) state->b->content) > *((int *) state->a->content) && n > 0) while (*((int *) state->b->content) > *((int *) state->a->content) && n > 0)
{ {
rra(state); ra(state);
n--; n--;
} }
pa(state); pa(state);