diff --git a/src/sort/sort5.c b/src/sort/sort5.c new file mode 100644 index 0000000..5098451 --- /dev/null +++ b/src/sort/sort5.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* sort5.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/07 15:17:36 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/07 15:57:09 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "push_swap.h" +#include + +int rotate_a_to_top(t_state *state) +{ + while (*(int *) state->a->content != stack_min(state->a)) + ra(state); + return (1); +} + +int merge_to_a(t_state *state) +{ + int n; + + n = ft_lstsize(state->a); + if (*(int *) state->b->content < stack_min(state->a)) + pa(state); + else if (*(int *) state->b->content > stack_max(state->a)) + { + rotate_a_to_top(state); + pa(state); + rra(state); + } + while (*((int *) state->b->content) > *((int *) state->a->content) && n > 0) + { + rra(state); + n--; + } + pa(state); + return (1); +} + +int sort5(t_state *state) +{ + pb(state); + pb(state); + 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); + return (1); +} diff --git a/src/util/stack_max.c b/src/util/stack_max.c new file mode 100644 index 0000000..8a97de0 --- /dev/null +++ b/src/util/stack_max.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* stack_max.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/07 16:29:41 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/07 16:36:40 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int stack_max(t_list *lst) +{ + int n; + + n = *(int *) lst->content; + while (lst->next) + { + if (*(int *) lst->content > n) + n = *(int *) lst->content; + lst = lst->next; + } + if (*(int *) lst->content > n) + n = *(int *) lst->content; + return (n); +} diff --git a/src/util/stack_min.c b/src/util/stack_min.c new file mode 100644 index 0000000..74e764b --- /dev/null +++ b/src/util/stack_min.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* stack_min.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/07 16:29:41 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/07 16:35:59 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int stack_min(t_list *lst) +{ + int n; + + n = *(int *) lst->content; + while (lst->next) + { + if (*(int *) lst->content < n) + n = *(int *) lst->content; + lst = lst->next; + } + if (*(int *) lst->content < n) + n = *(int *) lst->content; + return (n); +}