From 469320374d55690f9ae39312f22cd48443b152c8 Mon Sep 17 00:00:00 2001 From: Maoake Teriierooiterai Date: Thu, 15 Jan 2026 12:09:38 +0100 Subject: [PATCH] trying some opti start to opti --- Makefile | 2 +- algorithms/medium/medium_algo.c | 20 ++++++++--------- algorithms/medium/sort_utils.c | 13 +---------- algorithms/medium/sort_utils_two.c | 36 ++++++++---------------------- includes/medium_headers.h | 2 +- 5 files changed, 22 insertions(+), 51 deletions(-) diff --git a/Makefile b/Makefile index ec4d91c..d9f4608 100644 --- a/Makefile +++ b/Makefile @@ -93,7 +93,7 @@ NAME_BONUS = checker .PHONY: all clean fclean re bonus -all: $(NAME) $(NAME_BONUS) +all: $(NAME) $(NAME): $(OBJ) @$(CC) $(CFLAGS) $(OBJ) -o $(NAME) diff --git a/algorithms/medium/medium_algo.c b/algorithms/medium/medium_algo.c index 2de20a1..58ba0cd 100644 --- a/algorithms/medium/medium_algo.c +++ b/algorithms/medium/medium_algo.c @@ -13,25 +13,24 @@ #include "push_swap.h" #include "medium_headers.h" #include -#include + static int path_to_end(t_stacks *stacks, int max_range, int range, char c) { t_stack *tmp; + t_stack *start; int i; int first_pass; - if (c == 'a') - tmp = stacks->a; - else - tmp = stacks->b; + tmp = assign_stack(stacks, c); + start = assign_stack(stacks, c); tmp = tmp->previous; i = 0; first_pass = 1; - while (first_pass || tmp != stacks->b->previous) + while (first_pass || tmp != start->previous) { first_pass = 0; if (in_range(tmp->value, max_range, range)) - tmp = stacks->b; + tmp = start; tmp = tmp->previous; i++; } @@ -45,8 +44,8 @@ static int path_to_start(t_stacks *stacks, int max_range, int range, char c) int i; int first_pass; - tmp = (stacks, c); - start = (stacks, c); + tmp = assign_stack(stacks, c); + start = assign_stack(stacks, c); i = 0; first_pass = 1; while (first_pass || tmp != start) @@ -94,7 +93,8 @@ void bucket_algo(t_stacks *stacks, t_tab *preset, int range) t_tab *tmp; tmp = preset; - medium_pre_sort(stacks, preset, range); + while(stacks->a) + pb(stacks); while (preset) { push_range_to_b(stacks, preset, range); diff --git a/algorithms/medium/sort_utils.c b/algorithms/medium/sort_utils.c index 4c684ff..2b64157 100644 --- a/algorithms/medium/sort_utils.c +++ b/algorithms/medium/sort_utils.c @@ -77,18 +77,7 @@ void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range) { while (one_preset->nb_in > 0) { - if (wich_path(stacks, one_preset->max_range, range, 'b')) - { - while (!in_range(stacks->b->value, one_preset->max_range, range)) - rb(stacks); - } - else - { - while (!in_range(stacks->b->value, one_preset->max_range, range)) - { - rrb(stacks); - } - } + normal_move_path(stacks, one_preset, range); sort_little_pile(stacks); one_preset->nb_in--; } diff --git a/algorithms/medium/sort_utils_two.c b/algorithms/medium/sort_utils_two.c index c209c3d..ae14501 100644 --- a/algorithms/medium/sort_utils_two.c +++ b/algorithms/medium/sort_utils_two.c @@ -13,37 +13,19 @@ #include "push_swap.h" #include "medium_headers.h" -static void medium_pus_to_a(t_stacks *stacks, t_tab *one_preset, int range) +void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range) { - int i; - - i = 0; - while (one_preset->nb_in > i) + if (wich_path(stacks, one_preset->max_range, range, 'b')) { - if (wich_path(stacks, one_preset->max_range, range, 'a')) - { - while (!in_range(stacks->b->value, one_preset->max_range, range)) - ra(stacks); - } - else - { - while (!in_range(stacks->b->value, one_preset->max_range, range)) - rra(stacks); - } - pb(stacks); - i++; + while (!in_range(stacks->b->value, one_preset->max_range, range)) + rb(stacks); } -} - -void medium_pre_sort(t_stacks *stacks, t_tab *preset, int range) -{ - t_tab *tmp; - - tmp = preset; - while (preset) + else { - medium_pus_to_a(stacks, tmp, range); - tmp = tmp->next; + while (!in_range(stacks->b->value, one_preset->max_range, range)) + { + rrb(stacks); + } } } diff --git a/includes/medium_headers.h b/includes/medium_headers.h index 4def295..cc218ac 100644 --- a/includes/medium_headers.h +++ b/includes/medium_headers.h @@ -28,7 +28,7 @@ void bucket_algo(t_stacks *stacks, t_tab *preset, int range); void sort_from_left(t_stacks *stacks); void sort_from_right(t_stacks *stacks); void push_range_to_b(t_stacks *stacks, t_tab *one_preset, int range); -void medium_pre_sort(t_stacks *stacks, t_tab *preset, int range); +void normal_move_path(t_stacks *stacks, t_tab *one_preset, int range); t_stack *assign_stack(t_stacks *stacks, char c); /* MEDIUM UTILS FILES */ int range_bucket(t_stack *first);