From 2c3769be6816e04a5f1f8678c65aa4f661bbfd2e Mon Sep 17 00:00:00 2001 From: Willem Haffmans Date: Sun, 17 Nov 2024 10:44:21 +0000 Subject: [PATCH] sort5 --- inc/push_swap.h | 5 +++- src/sort/sort5.c | 30 ++++--------------- src/util/utils.c | 76 +++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 79 insertions(+), 32 deletions(-) diff --git a/inc/push_swap.h b/inc/push_swap.h index e92de28..1b4afeb 100644 --- a/inc/push_swap.h +++ b/inc/push_swap.h @@ -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); diff --git a/src/sort/sort5.c b/src/sort/sort5.c index 5e30124..925e807 100644 --- a/src/sort/sort5.c +++ b/src/sort/sort5.c @@ -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); } diff --git a/src/util/utils.c b/src/util/utils.c index e4b81ad..7a4764a 100644 --- a/src/util/utils.c +++ b/src/util/utils.c @@ -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);