diff --git a/Makefile b/Makefile index c6e3f5b..ce5dc8d 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,8 @@ PARS_DIR = parsing MEDIUM_DIR = medium +COMPLEX_DIR = radix + FLAGS_DIR = flags INCLUDES = headers @@ -34,6 +36,8 @@ STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len MEDIUM_ALGO = utils_medium.c utils_struct_tab.c utils_medium_two.c sort_utils.c sort_utils_two.c medium_algo.c +COMPLEX_ALGO = radix.c + ALGO_UTILS = check_order.c compare.c iterate.c pre_sort.c #============================ @@ -42,13 +46,13 @@ ALGO_UTILS = check_order.c compare.c iterate.c pre_sort.c ALL_FILES = $(SRC) $(STACK_UTILS_DIR)/$(STACK_UTILS) $(PARS_DIR)/$(PARSING) \ $(ALGO_DIR)/$(MEDIUM_DIR)/$(MEDIUM_ALGO) $(ALGO_UTILS_DIR)/$(ALGO_UTILS) \ - $(INSERT_DIR)/$(INSERTION) + $(INSERT_DIR)/$(INSERTION) $(ALGO_DIR)/$(COMPLEX_DIR)/$(COMPLEX_ALGO) OBJ_DIR = obj CC = cc -CFLAGS = -Wall -Werror -Wextra -I$(INCLUDES) +CFLAGS = -Wall -Werror -Wextra -g3 -I$(INCLUDES) NAME = push_swap @@ -83,6 +87,9 @@ $(OBJ_DIR)/%.o: $(ALGO_DIR)/%.c | $(OBJ_DIR) $(OBJ_DIR)/%.o: $(ALGO_DIR)/$(MEDIUM_DIR)/%.c | $(OBJ_DIR) $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ +$(OBJ_DIR)/%.o: $(ALGO_DIR)/$(COMPLEX_DIR)/%.c | $(OBJ_DIR) + $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ + $(OBJ_DIR)/%.o: $(ALGO_UTILS_DIR)/%.c | $(OBJ_DIR) $(CC) $(CFLAGS) -MMD -MP -c $< -o $@ @@ -102,4 +109,4 @@ fclean: clean re: fclean all --include $(DEP) \ No newline at end of file +-include $(DEP) diff --git a/algorithms/radix/radix.c b/algorithms/radix/radix.c new file mode 100644 index 0000000..31e8a82 --- /dev/null +++ b/algorithms/radix/radix.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* radix.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dgaillet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/01/06 12:47:06 by dgaillet #+# #+# */ +/* Updated: 2026/01/08 11:06:11 by dgaillet ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +static int still_unit_value(t_stacks *stacks, int unit) +{ + t_stack *temp; + + temp = stacks->a; + if (!temp || temp->value >= unit) + return (1); + temp = temp->next; + while (temp != stacks->a) + { + if (temp->value >= unit) + return (1); + temp = temp->next; + } + return (0); +} + +static void push_by_number_to_b(t_stacks *stacks, int unit, int nb) +{ + int i; + int s_len; + t_stack *temp; + + temp = stacks->a; + s_len = 1; + while (temp && temp->next != stacks->a) + { + s_len++; + temp = temp->next; + } + i = 0; + while (i < s_len && still_unit_value(stacks, unit)) + { + if (stacks->a && (stacks->a->value % (unit * 10)) / unit == nb) + pb(stacks); + else + ra(stacks); + i++; + } +} + +static void rec_sort(t_stacks *stacks, int unit) +{ + int i; + + i = 0; + if (!still_unit_value(stacks, unit)) + return ; + while (i <= 9) + { + push_by_number_to_b(stacks, unit, i); + i++; + } + while (stacks->b) + pa(stacks); + rec_sort(stacks, unit * 10); +} + +void radix(t_stacks *stacks) +{ + rec_sort(stacks, 1); +} diff --git a/headers/push_swap.h b/headers/push_swap.h index b358062..516b731 100644 --- a/headers/push_swap.h +++ b/headers/push_swap.h @@ -6,7 +6,7 @@ /* By: dgaillet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/08 14:18:06 by dgaillet #+# #+# */ -/* Updated: 2025/12/15 14:34:45 by dgaillet ### ########lyon.fr */ +/* Updated: 2026/01/07 10:30:42 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -66,4 +66,7 @@ int test2(char **argv); /* TEST FILE */ int test1(int argc, char **argv); +/* RADIX */ +void radix(t_stacks *stacks); + #endif diff --git a/test_one.c b/test_one.c index d451380..1a0ee0c 100644 --- a/test_one.c +++ b/test_one.c @@ -6,7 +6,7 @@ /* By: mteriier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/22 12:33:58 by mteriier #+# #+# */ -/* Updated: 2025/12/22 12:34:35 by mteriier ### ########.fr */ +/* Updated: 2026/01/07 14:58:10 by dgaillet ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int test1(int argc, char **argv) { t_stacks *piles; - t_tab *preset; + //t_tab *preset; piles = NULL; if (argc > 1)