Merge branch 'medium_algo'

This commit is contained in:
Maoake Teriierooiterai
2026-01-06 13:53:48 +01:00
16 changed files with 830 additions and 38 deletions

74
Makefile Normal file
View File

@@ -0,0 +1,74 @@
STACK_UTILS_DIR = stack_utils
ALGO_UTILS_DIR = algorithms/utils
ALGO_DIR = algorithms
MEDIUM_DIR = medium_utils
SRC = main.c ft_atoi.c parsing.c parsing_2.c test_one.c
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 sort_utils.c sort_utils_two.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)
OBJ_DIR = obj
CC = cc
CFLAGS = -Wall -Werror -Wextra -I.
NAME = push_swap
OBJ = $(addprefix $(OBJ_DIR)/, $(notdir $(ALL_FILES:.c=.o)))
DEP = $(OBJ:.o=.d)
.PHONY: all clean fclean re
all: $(NAME)
$(NAME): $(OBJ)
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
@echo "===================================="
@echo "======= PUSH SWAP COMPILED ========="
@echo "===================================="
$(OBJ_DIR)/%.o: %.c | $(OBJ_DIR)
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
$(OBJ_DIR)/%.o: $(STACK_UTILS_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
$(OBJ_DIR)/%.o: $(ALGO_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CFLAGS) -MMD -MP -c $< -o $@
$(OBJ_DIR)/%.o: $(ALGO_DIR)/$(MEDIUM_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 $@
$(OBJ_DIR):
@mkdir -p $(OBJ_DIR)
clean:
@rm -rf $(OBJ_DIR)
@echo "===================================="
@echo "= ALL OBJECT AND DEPENDENCES CLEAN ="
@echo "===================================="
fclean: clean
@rm -f $(NAME)
@echo "========== EXEC DELETED ============"
@echo "===================================="
re: fclean all
-include $(DEP)

115
algorithms/medium_algo.c Normal file
View File

@@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* medium_algo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 07:14:45 by mteriier #+# #+# */
/* Updated: 2025/12/10 07:14:51 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <stdlib.h>
int path_to_end(t_stacks *piles, int max_range, int range, char c)
{
t_stack *tmp;
int i;
int first_pass;
if (c == 'a')
tmp = piles->a;
else
tmp = piles->b;
tmp = tmp->previous;
i = 0;
first_pass = 1;
while (first_pass || tmp != piles->b->previous)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
tmp = piles->b;
tmp = tmp->previous;
i++;
}
return (i);
}
int path_to_start(t_stacks *piles, int max_range, int range, char c)
{
t_stack *tmp;
int i;
int first_pass;
if (c == 'a')
tmp = piles->a;
else
tmp = piles->b;
i = 0;
first_pass = 1;
while (first_pass || tmp != piles->b)
{
first_pass = 0;
if (in_range(tmp->value, max_range, range))
{
tmp = piles->b->previous;
}
tmp = tmp->next;
i++;
}
return (i);
}
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, c);
path_end = path_to_end(piles, max_range, range, c);
if (path_start < path_end)
return (1);
return (0);
}
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 (piles->a)
pb(piles);
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

@@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/05 14:11:49 by mteriier #+# #+# */
/* Updated: 2026/01/05 14:11:52 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void push_range_to_b(t_stacks *piles, t_tab *one_preset, int range)
{
while (one_preset->nb_in > 0)
{
if (wich_path(piles, one_preset->max_range, range, 'b'))
{
while (!in_range(piles->b->value, one_preset->max_range, range))
rb(piles);
}
else
{
while (!in_range(piles->b->value, one_preset->max_range, range))
{
rrb(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)
{
int start_path;
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);
return (0);
}
int number_move_reverse(t_stacks *piles, int value, char start_end)
{
int i;
t_stack *tmp;
i = 0;
tmp = piles->a;
if (start_end == 's')
{
while (tmp->value < value)
{
tmp = tmp->next;
i++;
}
}
else
{
tmp = tmp->previous;
while (tmp->value > value)
{
tmp = tmp->previous;
i++;
}
}
return (i);
}
void sort_little_pile(t_stacks *piles)
{
if (!piles->a)
{
pa(piles);
return ;
}
if (piles->a->previous->value < piles->b->value)
{
pa(piles);
ra(piles);
return ;
}
if (sort_path(piles, piles->b->value))
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->b->value > piles->a->value)
{
ra(piles);
i++;
}
pa(piles);
while (i > 0)
{
rra(piles);
i--;
}
}
void sort_from_right(t_stacks *piles)
{
int i;
i = 0;
while (piles->b->value < piles->a->previous->value)
{
rra(piles);
i++;
}
pa(piles);
while (i >= 0)
{
ra(piles);
i--;
}
}

View File

@@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_medium.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 13:58:40 by mteriier #+# #+# */
/* Updated: 2025/12/10 13:58:42 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int get_first_lower(t_stack *first)
{
t_stack *tmp;
int lower;
tmp = first;
lower = tmp->value;
while (tmp->next != first)
{
if (lower > tmp->value)
lower = tmp->value;
tmp = tmp->next;
}
return (lower);
}
int get_next_lower(t_stack *first, int old_lower)
{
t_stack *tmp;
int next_lower;
tmp = first;
next_lower = 2147483647;
while (tmp->next != first)
{
if (old_lower < tmp->value && tmp->value <= next_lower)
{
next_lower = tmp->value;
}
tmp = tmp->next;
}
return (next_lower);
}
int calcul_range(int value, int range)
{
int max_range;
max_range = 0;
if (value < 0)
while (max_range > value)
max_range -= range;
else
while (max_range <= value)
max_range += range;
if (max_range < 0)
return (max_range + range - 1);
return (max_range - 1);
}
int in_range(int value, int max_range, int range)
{
int min_range;
min_range = max_range - (range - 1);
if (value <= max_range && value >= min_range)
return (1);
return (0);
}
int get_number_in_range(int max_range, t_stack *a, int range)
{
int nb_in;
t_stack *tmp;
t_stack *first;
nb_in = 0;
tmp = a;
first = tmp;
while (tmp->next != first)
{
if (in_range(tmp->value, max_range, range))
nb_in++;
tmp = tmp->next;
}
if (in_range(tmp->value, max_range, range))
nb_in++;
return (nb_in);
}

View File

@@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_medium_two.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/22 09:36:56 by mteriier #+# #+# */
/* Updated: 2025/12/22 09:38:19 by mteriier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int get_max_number(t_stack *first)
{
int max;
t_stack *a;
a = first;
max = a->value;
while (a->next != first)
{
if (max < a->value)
max = a->value;
a = a->next;
}
return (max);
}
int get_min_number(t_stack *first)
{
int min;
t_stack *a;
a = first;
min = a->value;
while (a->next != first)
{
if (min > a->value)
min = a->value;
a = a->next;
}
return (min);
}
int my_sqrt(int nb)
{
int i;
if (nb < 1)
return (0);
i = 0;
while (i * i <= nb)
i++;
return (i);
}
int range_bucket(t_stack *first)
{
int len;
len = stack_len(first);
return ((get_max_number(first) - get_min_number(first)) / my_sqrt(len));
}

View File

@@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_struct_tab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 13:36:43 by mteriier #+# #+# */
/* Updated: 2025/12/10 13:36:48 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <stdlib.h>
t_tab *allocate_tab(int range_max, int nb)
{
t_tab *tab;
tab = malloc(sizeof(t_tab));
if (!tab)
return (NULL);
tab->next = NULL;
tab->max_range = range_max;
tab->nb_in = nb;
return (tab);
}
t_tab *get_tabs(t_stack *first, int range)
{
t_tab *tmp;
t_tab *first_tab;
int len_stack;
int scan_nb_in_tab;
len_stack = stack_len(first);
first_tab = init_first_tab(first, range);
if (!first_tab)
return (NULL);
scan_nb_in_tab = first_tab->nb_in;
tmp = first_tab;
while (scan_nb_in_tab < len_stack)
{
tmp->next = get_next_tab(first, tmp, range);
if (!(tmp->next))
return (free_tab(&first_tab));
tmp = tmp->next;
scan_nb_in_tab += tmp->nb_in;
}
return (first_tab);
}
t_tab *init_first_tab(t_stack *first, int range)
{
t_tab *tab;
int lower;
int range_max;
lower = get_first_lower(first);
range_max = calcul_range(lower, range);
tab = allocate_tab(range_max, get_number_in_range(range_max, first, range));
if (!tab)
return (NULL);
return (tab);
}
t_tab *get_next_tab(t_stack *first, t_tab *tab, int range)
{
int lower;
int range_max;
t_tab *next_tab;
lower = get_next_lower(first, tab->max_range);
range_max = calcul_range(lower, range);
next_tab = allocate_tab(range_max,
get_number_in_range(range_max, first, range));
return (next_tab);
}
t_tab *free_tab(t_tab **first)
{
if (!(*first))
return (NULL);
if ((*first)->next)
free_tab(&(*first)->next);
free(*first);
return (NULL);
}

View File

@@ -41,5 +41,3 @@ int is_highest(t_stack *stack, t_stack *node, int len)
}
return (1);
}

View File

@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* compare_value.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/10 11:08:35 by mteriier #+# #+# */
/* Updated: 2025/12/10 11:28:58 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int is_upper_compare(t_stack *t1, t_stack *t2)
{
if (t1->value > t2->value)
return (1);
return (0);
}

84
main.c
View File

@@ -13,6 +13,7 @@
#include "push_swap.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void print_all_stack(t_stack *stack, t_stack *first, char pile)
{
@@ -33,43 +34,58 @@ void print_all_stack(t_stack *stack, t_stack *first, char pile)
printf("[%d] \n", tmp->value);
}
int verif_no_double(int *tab, int len, int value)
{
int i;
i = 0;
while (i < len)
{
if (value == tab[i])
return (0);
i++;
}
return (1);
}
int adding_number(int *tab, int len)
{
int stock;
stock = tab[0];
while (!verif_no_double(tab, len, stock))
{
stock = 0 + rand() % (250 - 0 + 1);
}
return (stock);
}
int *auto_shuffle(int len_tab)
{
int *tab;
int i;
i = 1;
tab = malloc(len_tab * sizeof(int));
if (!tab)
return (NULL);
tab[0] = 0 + rand() % (250 - 0 + 1);
while (i < len_tab)
{
tab[i] = adding_number(tab, i - 1);
i++;
}
return (tab);
}
int main(int argc, char **argv)
{
t_stacks *stacks;
stacks = 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');
sa(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
pb(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
pa(stacks);
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');
pb(stacks);
pb(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
rrb(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
rrr(stacks);
print_all_stack(stacks->a, stacks->a, 'A');
print_all_stack(stacks->b, stacks->b, 'B');
*/
//print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
insertion(stacks, stack_a_len(stacks));
//print_stacks(stacks, highest_stack_len(stacks), stacks->a, stacks->b);
if (strcmp(argv[1], "-t2") == 0)
test2(argv++);
else
test1(argc, argv);
}
stack_clear_all(stacks->a, stacks->a);
stack_clear_all(stacks->b, stacks->b);
return (0);
}

View File

@@ -45,6 +45,8 @@ t_stacks *init_big_stacks(int argc, char **argv)
t_stack *a;
stacks = malloc(sizeof(t_stacks));
stacks->a = NULL;
stacks->b = NULL;
if (!stacks)
return (NULL);
a = parsing(argc, argv);

69
parsing_2.c Normal file
View File

@@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/22 13:10:58 by mteriier #+# #+# */
/* Updated: 2025/12/22 13:11:21 by mteriier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <stdlib.h>
#include <stdio.h>
void print_tabs(t_tab *preset)
{
t_tab *tab;
tab = preset;
while (tab)
{
printf("MAX RANGE : [%d]\n", tab->max_range);
printf("NUMBER IN : [%d]\n", tab->nb_in);
tab = tab->next;
}
}
t_stack *parsing2(int *tab, int len)
{
int i;
int stock;
t_stack *first;
t_stack *new;
i = 0;
first = NULL;
while (i < len)
{
stock = tab[i];
new = new_stack(stock);
if (!new && !first)
return (NULL);
else if (!new)
{
stack_clear_all(first, first);
return (NULL);
}
stack_add_back(&first, new);
i++;
}
return (first);
}
t_stacks *init_big_stacks2(int *tab, int len)
{
t_stacks *stacks;
t_stack *a;
stacks = malloc(sizeof(t_stacks));
stacks->a = NULL;
stacks->b = NULL;
if (!stacks)
return (NULL);
a = parsing2(tab, len);
stacks->a = a;
return (stacks);
}

BIN
push_swap Executable file

Binary file not shown.

View File

@@ -26,6 +26,13 @@ typedef struct s_stacks
t_stack *b;
} t_stacks;
typedef struct s_tab
{
int max_range;
int nb_in;
struct s_tab *next;
} t_tab;
/*STACK_FUNCTIONS*/
void pa(t_stacks *stacks);
void pb(t_stacks *stacks);
@@ -47,6 +54,47 @@ void stack_clear_all(t_stack *stack, t_stack *first);
t_stack *parsing(int argc, char **argv);
t_stacks *init_big_stacks(int argc, char **argv);
int ft_atoi(const char *nptr);
/*FUNCTION UTILS ALGO*/
int check_order(t_stack *stack);
int is_upper_compare(t_stack *t1, t_stack *t2);
int stack_len(t_stack *stack);
/*FUNCTION FOR MEDIUM ALGO*/
t_tab *get_tabs(t_stack *first, int range);
int get_number_in_range(int max_range, t_stack *a, int range);
int in_range(int value, int max_range, int range);
int calcul_range(int value, int range);
int get_next_lower(t_stack *first, int old_lower);
int get_first_lower(t_stack *first);
int my_sqrt(int nb);
int get_max_number(t_stack *first);
int get_min_number(t_stack *first);
int range_bucket(t_stack *first);
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, 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);
int adding_number(int *tab, int len);
int *auto_shuffle(int len_tab);
/*FUNCTION IN FILE TEST*/
int test1(int argc, char **argv);
int test2(char **argv);
/*FUNCTION IN PARSIN 2*/
t_stack *parsing2(int *tab, int len);
t_stacks *init_big_stacks2(int *tab, int len);
void print_tabs(t_tab *preset);
void print_stacks(t_stacks *stacks, int len, t_stack *a, t_stack *b);
int stack_a_len(t_stacks *stacks);
int stack_b_len(t_stacks *stacks);

View File

@@ -33,8 +33,6 @@ void pa(t_stacks *stacks)
write(1, "pa\n", 3);
}
#include <stdio.h>
void pb(t_stacks *stacks)
{
t_stack *a_push;

61
test_one.c Normal file
View File

@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_one.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.lyon42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/22 12:33:58 by mteriier #+# #+# */
/* Updated: 2025/12/22 12:34:35 by mteriier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include <stdio.h>
#include <stdlib.h>
int test1(int argc, char **argv)
{
t_stacks *piles;
t_tab *preset;
piles = NULL;
if (argc > 1)
{
piles = init_big_stacks(argc, argv);
preset = get_tabs(piles->a, range_bucket(piles->a));
bucket_algo(piles, preset, range_bucket(piles->a));
}
if (piles->a)
stack_clear_all(piles->a, piles->a);
if (piles->b)
stack_clear_all(piles->b, piles->b);
free(piles);
return (0);
}
int test2(char **argv)
{
int *tab;
int len;
t_tab *preset;
t_stacks *piles;
len = ft_atoi(argv[2]);
if (len < 1)
{
printf("WRONG LEN PLS BE SMART.\n");
return (0);
}
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);
if (piles->a)
stack_clear_all(piles->a, piles->a);
if (piles->b)
stack_clear_all(piles->b, piles->b);
free(piles);
return (0);
}