From 79b79c2b7552b4885384c4c60c34ba23e490e350 Mon Sep 17 00:00:00 2001 From: Corentin Lefrere Date: Tue, 9 Dec 2025 16:07:23 +0100 Subject: [PATCH] Adding selection sort algorithm --- Makefile | 2 +- algorithm_selection_sort.c | 33 +++++++++++++++++++++++++++++++++ main_pushswap.c | 4 ++-- pushswap.h | 5 +++-- 4 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 algorithm_selection_sort.c diff --git a/Makefile b/Makefile index 33bd73b..2ba8855 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ NAME=push_swap -SRCS=algorithm_quicksort.c cheatalloc.c clist.c list.c main_pushswap.c op_output.c op_p.c op_r.c op_rr.c op_s.c ops.c ops_groups.c ops_optimizer.c output.c psval.c stack.c stacks.c +SRCS=algorithm_quicksort.c algorithm_selection_sort.c cheatalloc.c clist.c list.c main_pushswap.c op_output.c op_p.c op_r.c op_rr.c op_s.c ops.c ops_groups.c ops_optimizer.c output.c psval.c stack.c stacks.c OBJS=${SRCS:%.c=${OBJDIR}/%.o} diff --git a/algorithm_selection_sort.c b/algorithm_selection_sort.c new file mode 100644 index 0000000..136dca4 --- /dev/null +++ b/algorithm_selection_sort.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* algorithm_selection_sort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: clefrere +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/12/09 15:43:09 by clefrere #+# #+# */ +/* Updated: 2025/12/09 15:59:47 by clefrere ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pushswap.h" + +void algorithm_selection_sort(const t_stacks *stacks, t_closure cb) +{ + t_list sorted; + + sorted = list_new(stacks->a); + list_sort(sorted); + while (sorted.len) + { + if (stacks->a.list->val == sorted.ptr[0]) + { + (cb.func)(cb.data, OP_PB); + sorted = list_sub(sorted, 1, sorted.len); + } + else + (cb.func)(cb.data, OP_RA); + } + while (stacks->b.len) + (cb.func)(cb.data, OP_PA); +} diff --git a/main_pushswap.c b/main_pushswap.c index b5610a2..8fc7ba6 100644 --- a/main_pushswap.c +++ b/main_pushswap.c @@ -6,7 +6,7 @@ /* By: clefrere +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/02 22:15:12 by agilliar #+# #+# */ -/* Updated: 2025/12/08 20:22:37 by agilliar ### ########.fr */ +/* Updated: 2025/12/09 16:06:44 by clefrere ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ static bool ft_streq(char *str1, char *str2) static void arg_step(char *str, t_args *arg) { if (ft_streq(str, "--simple")) - arg->algo = NULL; + arg->algo = &algorithm_selection_sort; else if (ft_streq(str, "--medium")) arg->algo = NULL; else if (ft_streq(str, "--complex")) diff --git a/pushswap.h b/pushswap.h index 98cb791..c4b98ca 100644 --- a/pushswap.h +++ b/pushswap.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* pushswap.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: agilliar +#+ +:+ +#+ */ +/* By: clefrere +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/12/02 11:02:44 by agilliar #+# #+# */ -/* Updated: 2025/12/09 14:50:45 by agilliar ### ########.fr */ +/* Updated: 2025/12/09 15:54:48 by clefrere ### ########.fr */ /* */ /* ************************************************************************** */ @@ -146,6 +146,7 @@ t_list list_new(t_stack stack); t_list list_sub(t_list self, size_t start, size_t end); void list_sort(t_list list); +void algorithm_selection_sort(const t_stacks *stacks, t_closure cb); void algorithm_quicksort(const t_stacks *stacks, t_closure cb); #endif -- 2.51.0