8 Commits

Author SHA1 Message Date
27a6a4806b fix segfault 2026-01-08 11:09:21 +00:00
c799dd65ee Radix is woking 2026-01-07 16:21:52 +00:00
2c6d8d4095 Merge branch 'master' into radix 2026-01-07 14:53:07 +00:00
David Gailleton
6942548108 Not working radix 2026-01-07 14:50:04 +00:00
Maoake Teriierooiterai
17f9a5affd delete the parsing2.c cause no use for now 2026-01-07 13:27:06 +01:00
Maoake Teriierooiterai
e1351b4d2f doing the norm 2026-01-07 13:23:42 +01:00
Maoake Teriierooiterai
93e8c10b27 adding the flags 2026-01-07 13:13:30 +01:00
Maoake Teriierooiterai
0d9b46c748 fix calcul_range function cause i didnt manage to scan the last value on the pile 2026-01-07 12:03:57 +01:00
17 changed files with 240 additions and 76 deletions

2
.gitignore vendored
View File

@@ -58,7 +58,7 @@ dkms.conf
*.swp
# Executable
pushswap
push_swap
# File obj
obj/

View File

@@ -14,6 +14,10 @@ PARS_DIR = parsing
MEDIUM_DIR = medium
COMPLEX_DIR = radix
FLAGS_DIR = flags
INCLUDES = headers
#============================
@@ -24,12 +28,16 @@ SRC = main.c test_one.c
INSERTION = insertion.c
PARSING = ft_atoi.c parsing.c parsing_2.c
FLAGS_FILES = algorithms_sort.c flag.c
PARSING = ft_atoi.c parsing.c ft_strncmp.c
STACK_UTILS = push.c rev_rotate.c rotate.c stack_add.c stack_remove.c stacks_len.c swap.c print_stacks.c
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
#============================
@@ -38,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
@@ -79,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 $@

View File

@@ -24,7 +24,8 @@ static int to_insert(t_stacks *stacks, int sorted)
a = stacks->a;
while (i < sorted)
{
if (stacks->b->value > a->previous->value && stacks->b->value <= a->value)
if (stacks->b->value > a->previous->value
&& stacks->b->value <= a->value)
return (i);
a = a->previous;
i++;

View File

@@ -32,11 +32,14 @@ int get_next_lower(t_stack *first, int old_lower)
{
t_stack *tmp;
int next_lower;
int skip_first;
tmp = first;
skip_first = 1;
next_lower = 2147483647;
while (tmp->next != first)
while (tmp != first || skip_first)
{
skip_first = 0;
if (old_lower < tmp->value && tmp->value <= next_lower)
{
next_lower = tmp->value;

View File

@@ -64,7 +64,7 @@ int range_bucket(t_stack *first)
int sqrt;
len = stack_len(first);
diff = (get_max_number(first) - get_min_number(first)) ;
diff = get_max_number(first) - get_min_number(first);
sqrt = my_sqrt(len);
if (diff / sqrt < 2)
{

76
algorithms/radix/radix.c Normal file
View File

@@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* radix.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

42
flags/algorithms_sort.c Normal file
View File

@@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* algorithms_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/07 12:15:02 by mteriier #+# #+# */
/* Updated: 2026/01/07 12:15:05 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "medium_algo.h"
void simple(t_stacks *piles)
{
int len;
len = stack_a_len(piles);
insertion(piles, len);
}
void medium(t_stacks *piles)
{
t_tab *buckets;
int range;
range = range_bucket(piles->a);
buckets = get_tabs(piles->a, range);
bucket_algo(piles, buckets, range);
}
void complex(t_stacks *piles)
{
return ;
}
void adaptive(t_stacks *piles)
{
return ;
}

28
flags/flag.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* flag.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/07 12:39:29 by mteriier #+# #+# */
/* Updated: 2026/01/07 12:39:31 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
#include "parsing.h"
void flags(int pos, char **argv, t_stacks *piles)
{
if (ft_strncmp(argv[pos], "--simple", 30) && pos > 0)
simple(piles);
else if (ft_strncmp(argv[pos], "--medium", 30) && pos > 0)
medium(piles);
else if (ft_strncmp(argv[pos], "--complex", 30) && pos > 0)
complex(piles);
else if (ft_strncmp(argv[pos], "--adaptive", 30) && pos > 0)
adaptive(piles);
else
adaptive(piles);
}

22
headers/flags.h Normal file
View File

@@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* flags.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/07 13:05:52 by mteriier #+# #+# */
/* Updated: 2026/01/07 13:05:53 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
#ifndef FLAGS_H
# define FLAGS_H
void simple(t_stacks *piles);
void medium(t_stacks *piles);
void complex(t_stacks *piles);
void adaptive(t_stacks *piles);
void flags(int pos, char **argv, t_stacks *piles);
#endif

View File

@@ -14,7 +14,7 @@
# define PARSING_H
int ft_atoi(const char *nptr);
t_stacks *init_big_stacks2(int *tab, int len);
t_stacks *init_big_stacks(int argc, char **argv);
int ft_strncmp(const char *s1, const char *s2, int n);
#endif

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
@@ -45,6 +45,7 @@ t_stack *new_stack(int value);
void stack_add_back(t_stack **stack, t_stack *new);
void stack_add_front(t_stack **stack, t_stack *new);
void stack_clear_all(t_stack *stack, t_stack *first);
void free_all(t_stacks *piles);
/* STACKS LEN FILES */
int stack_a_len(t_stacks *stacks);
int stack_b_len(t_stacks *stacks);
@@ -65,4 +66,7 @@ int test2(char **argv);
/* TEST FILE */
int test1(int argc, char **argv);
/* RADIX */
void radix(t_stacks *stacks);
#endif

25
parsing/ft_strncmp.c Normal file
View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/07 12:36:38 by mteriier #+# #+# */
/* Updated: 2026/01/07 12:36:40 by mteriier ### ########lyon.fr */
/* */
/* ************************************************************************** */
int ft_strncmp(const char *s1, const char *s2, int n)
{
int i;
i = 0;
if (n == 0)
return (0);
while (i < n - 1 && s1[i] && s1[i] == s2[i])
{
i++;
}
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
}

View File

@@ -1,55 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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>
static 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

@@ -21,3 +21,12 @@ void stack_clear_all(t_stack *stack, t_stack *first)
stack_clear_all(stack->next, first);
free(stack);
}
void free_all(t_stacks *piles)
{
if (piles->a)
stack_clear_all(piles->a, piles->a);
if (piles->b)
stack_clear_all(piles->b, piles->b);
free(piles);
}

View File

@@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2026/01/07 14:58:10 by dgaillet ### ########lyon.fr */
/* */
/* ************************************************************************** */
@@ -19,19 +19,17 @@
int test1(int argc, char **argv)
{
t_stacks *piles;
t_tab *preset;
//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));
//preset = get_tabs(piles->a, range_bucket(piles->a));
//bucket_algo(piles, preset, range_bucket(piles->a));
radix(piles);
}
if (piles->a)
stack_clear_all(piles->a, piles->a);
if (piles->b)
stack_clear_all(piles->b, piles->b);
free(piles);
free_all(piles);
//free(piles);
return (0);
}