From 7f5de6c4fdebe557ada85dc00ddf626a668f1a1e Mon Sep 17 00:00:00 2001 From: whaffman Date: Sun, 17 Nov 2024 13:25:59 +0100 Subject: [PATCH] create put at index asc and desc --- src/push_swap.c | 4 +-- src/sort/sort3.c | 13 +++------- src/sort/sort5.c | 1 - src/util/utils.c | 65 ++++++++++++++++++------------------------------ 4 files changed, 29 insertions(+), 54 deletions(-) diff --git a/src/push_swap.c b/src/push_swap.c index 1a9cee5..41cfca2 100644 --- a/src/push_swap.c +++ b/src/push_swap.c @@ -70,8 +70,8 @@ int main(int argc, char *argv[]) return (1); } } - ft_printf("min: %d\n", stack_min(state->a)); - ft_printf("max: %d\n", stack_max(state->a)); + // ft_printf("min: %d\n", stack_min(state->a)); + // ft_printf("max: %d\n", stack_max(state->a)); sort5(state); print_state(state); return (0); diff --git a/src/sort/sort3.c b/src/sort/sort3.c index 0d918bc..73ef666 100644 --- a/src/sort/sort3.c +++ b/src/sort/sort3.c @@ -30,19 +30,12 @@ int sort3(t_state *state) return (0); if (a < b && b < c) return (1); - else if (a < b && b > c) + else if (a < b && b > c && a < c) { rra(state); - if (a < c) - sa(state); - } - else if (a > c && b < c) - ra(state); - else - { sa(state); - if (b > c) - rra(state); } + else if (!(a > c && b < c)) + sa(state); return (1); } diff --git a/src/sort/sort5.c b/src/sort/sort5.c index 925e807..b547dde 100644 --- a/src/sort/sort5.c +++ b/src/sort/sort5.c @@ -39,6 +39,5 @@ int sort5(t_state *state) merge_to_a(state); merge_to_a(state); rotate_a_to_top(state); - return (1); } diff --git a/src/util/utils.c b/src/util/utils.c index 7a4764a..43fcc90 100644 --- a/src/util/utils.c +++ b/src/util/utils.c @@ -10,6 +10,7 @@ /* */ /* ************************************************************************** */ +#include "libft.h" #include "push_swap.h" int shortest_rotate(int index, int size) @@ -23,7 +24,7 @@ int shortest_rotate(int index, int size) void rotate_a_to(int index, t_state *state) { int n; - + n = shortest_rotate(index, ft_lstsize(state->a)); while (n > 0) { @@ -39,7 +40,7 @@ void rotate_a_to(int index, t_state *state) void rotate_b_to(int index, t_state *state) { int n; - + n = shortest_rotate(index, ft_lstsize(state->b)); while (n > 0) { @@ -75,52 +76,34 @@ int ft_min(int a, int b) return (b); } -int put_at_index_desc(int number, t_list *stack) +int count_gt(int n, t_list *stack) { - int i; + int count; - 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 + count = 0; + while (stack) { - 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 (*(int *) stack->content > n) + count++; + stack = stack->next; } - if (i == ft_lstsize(stack)) - return (0); - return (i); + return (count); } int put_at_index_asc(int number, t_list *stack) { - int i; + const int i = index_of(stack_min(stack), stack); + const int size = ft_lstsize(stack); + const int gt = count_gt(number, stack); - i = 0; - if (number < stack_min(stack) || number > stack_max(stack)) - i = index_of(stack_min(stack), stack); - else - { - 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); - return (i); + return ((i + gt) % size); +} + +int put_at_index_desc(int number, t_list *stack) +{ + const int i = index_of(stack_max(stack), stack); + const int size = ft_lstsize(stack); + const int lt = size - count_gt(number, stack); + + return ((i + lt) % size); }