finish the medium need to modify and sort in a just need to do the reverse

This commit is contained in:
Maoake Teriierooiterai
2026-01-06 12:41:36 +01:00
parent ae4740c479
commit f98dce6f61
7 changed files with 143 additions and 44 deletions

View File

@@ -12,9 +12,9 @@ STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c swap.c
ALGO_SORT = medium_algo.c
MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c
MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c sort_utils.c sort_utils_two.c
ALGO_UTILS = check_order.c compare_value.c stack_len.c
ALGO_UTILS = check_order.c compare_value.c
ALL_FILES = $(SRC) $(STACK_UTILS_DIR)/$(STACK_UTILS) $(ALGO_DIR)/$(ALGO_SORT) \
$(ALGO_DIR)/$(MEDIUM_DIR)/$(MEDIUM_ALGO) $(ALGO_UTILS_DIR)/$(ALGO_UTILS)

View File

@@ -11,61 +11,103 @@
/* ************************************************************************** */
#include "push_swap.h"
#include <stdlib.h>
int path_to_end(t_stacks *piles, int max_range, int range)
int path_to_end(t_stacks *piles, int max_range, int range, char c)
{
t_stack *tmp;
int i;
int first_pass;
tmp = piles->a;
if (c == 'a')
tmp = piles->a;
else
tmp = piles->b;
tmp = tmp->previous;
i = 0;
first_pass = 1
first_pass = 1;
while (first_pass || tmp != piles->a->previous)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
tmp = piles->a->previous;
tmp = piles->a;
tmp = tmp->previous;
i++;
}
return (i);
}
int path_to_start(t_stacks *piles, int max_range, int range)
int path_to_start(t_stacks *piles, int max_range, int range, char c)
{
t_stack *tmp;
int i;
int first_pass;
tmp = piles ->a;
if (c == 'a')
tmp = piles->a;
else
tmp = piles->b;
i = 0;
first_pass = 1;
while (first_pass || tmp != piles->a)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
tmp = piles->a;
{
tmp = piles->a->previous;
}
tmp = tmp->next;
i++;
}
return (i);
}
int wich_path(t_stacks *piles, int max_range, int range)
int wich_path(t_stacks *piles, int max_range, int range, char c)
{
int path_start;
int path_end;
path_start = path_to_start(piles, max_range, range);
path_end = path_to_end(piles, max_range, range);
path_start = path_to_start(piles, max_range, range, c);
path_end = path_to_end(piles, max_range, range, c);
if (path_start < path_end)
return (1);
return (0);
}
void bucket_algo(void)
int stack_len(t_stack *stack)
{
t_stack *first;
int i;
first = stack;
i = 1;
while (first->next != stack)
{
first = first->next;
i++;
}
return (i);
}
void bucket_algo(t_stacks *piles, t_tab *preset, int range)
{
t_tab *tmp;
tmp = preset;
while (preset)
{
push_range_to_b(piles, preset, range);
if (preset->next)
tmp = preset->next;
else
tmp = NULL;
if (preset)
free(preset);
preset = tmp;
}
while (piles->b)
pa(piles);
print_all_stack(piles->a, piles->a, 'A');
print_all_stack(piles->b, piles->b, 'B');
return ;
}

View File

@@ -14,23 +14,25 @@
void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range)
{
t_stack *a;
t_stack *b;
a = piles->a;
b = piles->b;
while (one_preset->nb_in > 0)
{
if (wich_path(piles, one_preset->max_range, range, 'a'))
{
while (!in_range(a->value))
while (!in_range(piles->a->value, one_preset->max_range, range))
ra(piles);
}
else
while (!in_range(a->value))
{
while (!in_range(piles->a->value, one_preset->max_range, range))
{
rra(piles);
pb(piles);
}
}
sort_little_pile(piles);
one_preset->nb_in--;
}
print_all_stack(piles->a, piles->a, 'A');
print_all_stack(piles->b, piles->b, 'B');
}
int sort_path(t_stacks *piles, int value)
@@ -39,6 +41,8 @@ int sort_path(t_stacks *piles, int value)
int end_path;
start_path = number_move_reverse(piles, value, 's');
if (start_path == 0)
return (1);
end_path = number_move_reverse(piles, value, 'e');
if (start_path < end_path)
return (1);
@@ -54,7 +58,7 @@ int number_move_reverse(t_stacks *piles, int value, char start_end)
tmp = piles->b;
if (start_end == 's')
{
while(tmp->value > value)
while(tmp->value < value)
{
tmp = tmp->next;
i++;
@@ -63,7 +67,7 @@ int number_move_reverse(t_stacks *piles, int value, char start_end)
else
{
tmp = tmp->previous;
while(tmp->value < value)
while(tmp->value > value)
{
tmp = tmp->previous;
i++;
@@ -74,9 +78,6 @@ int number_move_reverse(t_stacks *piles, int value, char start_end)
void sort_little_pile(t_stacks *piles)
{
int i;
i = 0;
if (!piles->b)
{
pb(piles);
@@ -89,5 +90,7 @@ void sort_little_pile(t_stacks *piles)
return ;
}
if (sort_path(piles, piles->a->value))
//you stop here
sort_from_left(piles);
else
sort_from_right(piles);
}

View File

@@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_utils_two.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/06 07:52:36 by mteriier #+# #+# */
/* Updated: 2026/01/06 07:52:38 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void sort_from_left(t_stacks *piles)
{
int i;
i = 0;
while (piles->a->value > piles->b->value)
{
rb(piles);
i++;
}
pb(piles);
while (i > 0)
{
rrb(piles);
i--;
}
}
void sort_from_right(t_stacks *piles)
{
int i;
i = 0;
while (piles->a->value < piles->b->previous->value)
{
rrb(piles);
i++;
}
pb(piles);
while (i >= 0)
{
rb(piles);
i--;
}
}

BIN
push_swap Executable file

Binary file not shown.

View File

@@ -73,9 +73,16 @@ t_tab *free_tab(t_tab **first);
t_tab *get_next_tab(t_stack *first, t_tab *tab, int range);
t_tab *init_first_tab(t_stack *first, int range);
t_tab *allocate_tab(int range_max, int nb);
int wich_path(t_stacks *piles, int max_range, int range);
int path_to_start(t_stacks *piles, int max_range, int range);
int path_to_end(t_stacks *piles, int max_range, int range);
int wich_path(t_stacks *piles, int max_range, int range, char c);
int path_to_start(t_stacks *piles, int max_range, int range, char c);
int path_to_end(t_stacks *piles, int max_range, int range, char c);
void bucket_algo(t_stacks *piles, t_tab *preset, int range);
void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range);
int sort_path(t_stacks *piles, int value);
int number_move_reverse(t_stacks *piles, int value, char start_end);
void sort_little_pile(t_stacks *piles);
void sort_from_left(t_stacks *piles);
void sort_from_right(t_stacks *piles);
/*FUNCTION IN MAIN*/
void print_all_stack(t_stack *stack, t_stack *first, char pile);
int verif_no_double(int *tab, int len, int value);

View File

@@ -16,23 +16,21 @@
int test1(int argc, char **argv)
{
t_stacks *stacks;
t_stacks *piles;
t_tab *preset;
stacks = NULL;
piles = NULL;
if (argc > 1)
{
stacks = init_big_stacks(argc, argv);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
rra(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
piles = init_big_stacks(argc, argv);
preset = get_tabs(piles->a, range_bucket(piles->a));
bucket_algo(piles, preset, range_bucket(piles->a));
}
if (stacks->a)
stack_clear_all(stacks->a, stacks->a);
if (stacks->b)
stack_clear_all(stacks->b, stacks->b);
free(stacks);
if (piles->a)
stack_clear_all(piles->a, piles->a);
if (piles->b)
stack_clear_all(piles->b, piles->b);
free(piles);
return (0);
}
@@ -52,8 +50,8 @@ int test2(char **argv)
tab = auto_shuffle(len);
piles = init_big_stacks2(tab, len);
preset = get_tabs(piles->a, range_bucket(piles->a));
bucket_algo(piles, preset, range_bucket(piles->a));
free(tab);
free_tab(&preset);
if (piles->a)
stack_clear_all(piles->a, piles->a);
if (piles->b)