]> Untitled Git - axy/ft/pushswap.git/commitdiff
Adding selection sort algorithm
authorCorentin Lefrere <clefrere@k0r4p10.42mulhouse.fr>
Tue, 9 Dec 2025 15:07:23 +0000 (16:07 +0100)
committerCorentin Lefrere <clefrere@k0r4p10.42mulhouse.fr>
Tue, 9 Dec 2025 15:07:23 +0000 (16:07 +0100)
Makefile
algorithm_selection_sort.c [new file with mode: 0644]
main_pushswap.c
pushswap.h

index 33bd73b2f8a4a574df39d209d58a4a5d2825032a..2ba885562932d3b64a6bd0b9e51f3aa645b910d6 100644 (file)
--- 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 (file)
index 0000000..136dca4
--- /dev/null
@@ -0,0 +1,33 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   algorithm_selection_sort.c                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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);
+}
index b5610a2d5f32ef31f558f2766e70150fd8298e63..8fc7ba6ed871a4b8705c0788a541ab3896a92fb7 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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"))
index 98cb79192176774e36c76e1170863e15032c649a..c4b98cabb38f9136e16e8ef090b903fe833abc14 100644 (file)
@@ -3,10 +3,10 @@
 /*                                                        :::      ::::::::   */
 /*   pushswap.h                                         :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
-/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
+/*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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