]> Untitled Git - axy/ft/pushswap.git/commitdiff
Medium algorithm
author= <=>
Thu, 18 Dec 2025 13:14:40 +0000 (14:14 +0100)
committer= <=>
Thu, 18 Dec 2025 13:14:40 +0000 (14:14 +0100)
Makefile
algorithm_medium.c [moved from algorithm_leafsort.c with 57% similarity]
frac.c
main_pushswap.c
pushswap.h

index 831306a8f3c62b0c73277cb131db0d3c971781c9..ee8dee5ab3ed544b7cafb7fe0e2407cb55cd093c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 NAME=push_swap
 
-SRCS=algorithm_complex.c algorithm_leafsort.c algorithm_simple.c cheatalloc.c clist.c closure.c disorder.c frac.c leaf_sort.c leaf_sort_index.c leaf_sort_parse.c list.c main_pushswap.c op_output.c op_p.c op_parse.c op_r.c op_rr.c op_s.c ops.c ops_count.c ops_optimizer.c ops_stackops.c output.c psval.c slice.c splitsort.c splitsort_final.c splitsort_iter.c splitsort_part.c stack.c stacks.c stacks_get.c
+SRCS=algorithm_complex.c algorithm_medium.c algorithm_simple.c cheatalloc.c clist.c closure.c disorder.c frac.c leaf_sort.c leaf_sort_index.c leaf_sort_parse.c list.c main_pushswap.c op_output.c op_p.c op_parse.c op_r.c op_rr.c op_s.c ops.c ops_count.c ops_optimizer.c ops_stackops.c output.c psval.c slice.c splitsort.c splitsort_final.c splitsort_iter.c splitsort_part.c stack.c stacks.c stacks_get.c
 
 RESSRCS=_res_leaf_sort_lookup.h
 
similarity index 57%
rename from algorithm_leafsort.c
rename to algorithm_medium.c
index 6e362398e19e2fc2a1ffa8d0fb7b2ae0fe7d6d1e..aa9aabcf74d54fc37dc64b69378156aa465ae11f 100644 (file)
@@ -1,18 +1,28 @@
 /* ************************************************************************** */
 /*                                                                            */
 /*                                                        :::      ::::::::   */
-/*   algorithm_leafsort.c                               :+:      :+:    :+:   */
+/*   algorithm_medium.c                                 :+:      :+:    :+:   */
 /*                                                    +:+ +:+         +:+     */
-/*   By: agilliar <agilliar@student.42mulhouse.fr>  +#+  +:+       +#+        */
+/*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
-/*   Created: 2025/12/11 01:00:50 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/15 20:36:32 by agilliar         ###   ########.fr       */
+/*   Created: 2025/12/18 14:04:08 by agilliar          #+#    #+#             */
+/*   Updated: 2025/12/18 14:08:12 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "pushswap.h"
 
-void   algorithm_leafsort(const t_stacks *stacks, t_closure cb)
+static size_t  size_square(size_t n)
 {
-       leaf_top_a_a(stacks, cb, stacks->a.len);
+       return (n * n);
+}
+
+void   algorithm_medium(const t_stacks *stacks, t_closure cb)
+{
+       size_t  chunks;
+
+       chunks = 5;
+       while (size_square(chunks * 10) < stacks->a.len)
+               chunks++;
+       splitsort(stacks, cb, chunks);
 }
diff --git a/frac.c b/frac.c
index 449a18d533d9f9e2b59fbd555ef87fcbaa2ab13c..2a1addd879d3c85e31485863d71cc49d773f4cbe 100644 (file)
--- a/frac.c
+++ b/frac.c
@@ -6,7 +6,7 @@
 /*   By: agilliar <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/18 13:20:12 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/18 13:27:56 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/18 13:51:28 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -26,7 +26,7 @@ t_frac        frac_mul(t_frac lhs, t_frac rhs)
        return (frac_new(lhs.num * rhs.num, lhs.den * rhs.den));
 }
 
-bool   frac_lte(t_frac lhs, t_frac rhs)
+bool   frac_lt(t_frac lhs, t_frac rhs)
 {
-       return (lhs.num * rhs.den <= rhs.num * lhs.den);
+       return (lhs.num * rhs.den < rhs.num * lhs.den);
 }
index 8aee6128b7ec038fb9994e08e16060c2e9dff57a..e46902f07617c5b1761ab23f68b98ea1c70ab94d 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 22:15:12 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/16 20:28:03 by agilliar         ###   ########.fr       */
+/*   Updated: 2025/12/18 14:07:38 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -36,15 +36,10 @@ typedef struct s_args
        t_algovar       algo;
        t_algorithm     *algo_func;
        bool            bench;
-       float           disorder;
+       t_frac          disorder;
 }      t_args;
 
-void algorithm_medium(const t_stacks *stacks, t_closure cb)
-{
-       algorithm_complex(stacks, cb);
-}
-
-static t_algorithm     *algorithm_get(t_algovar var, float disorder)
+static t_algorithm     *algorithm_get(t_algovar var, t_frac disorder)
 {
        if (var == ALGORITHM_SIMPLE)
                return (&algorithm_simple);
@@ -52,9 +47,9 @@ static t_algorithm    *algorithm_get(t_algovar var, float disorder)
                return (&algorithm_medium);
        if (var == ALGORITHM_COMPLEX)
                return (&algorithm_complex);
-       if (disorder < 0.2)
+       if (frac_lt(disorder, frac_new(1, 5)))
                return (&algorithm_simple);
-       if (disorder < 0.5)
+       if (frac_lt(disorder, frac_new(1, 2)))
                return (&algorithm_medium);
        return (&algorithm_complex);
 }
@@ -66,7 +61,7 @@ static t_algorithm    *algorithm_get(t_algovar var, float disorder)
 #define COMPLEXITY_ADAPTIVE_MEDIUM "Adaptive / O(N^(3/2))"
 #define COMPLEXITY_ADAPTIVE_HIGH "Adaptive / O(N log N)"
 
-t_slice        complexity_get(t_algovar var, float disorder)
+t_slice        complexity_get(t_algovar var, t_frac disorder)
 {
        if (var == ALGORITHM_SIMPLE)
                return (slice_new(COMPLEXITY_SIMPLE));
@@ -74,9 +69,9 @@ t_slice       complexity_get(t_algovar var, float disorder)
                return (slice_new(COMPLEXITY_MEDIUM));
        if (var == ALGORITHM_COMPLEX)
                return (slice_new(COMPLEXITY_COMPLEX));
-       if (disorder < 0.2)
+       if (frac_lt(disorder, frac_new(1, 5)))
                return (slice_new(COMPLEXITY_ADAPTIVE_LOW));
-       if (disorder < 0.5)
+       if (frac_lt(disorder, frac_new(1, 2)))
                return (slice_new(COMPLEXITY_ADAPTIVE_MEDIUM));
        return (slice_new(COMPLEXITY_ADAPTIVE_HIGH));
 }
@@ -102,7 +97,7 @@ typedef struct       s_benchres
        t_ops_count     pre_opt;
        t_ops_count     post_opt;
        t_slice         strategy;
-       float           disorder;
+       t_frac          disorder;
 }      t_benchres;
 
 static t_benchres      pushswap_solve(t_args *args)
@@ -131,14 +126,16 @@ int       main(int argc, char **argv)
        args.state = stacks_new();
        args.bench = false;
        args.algo = ALGORITHM_ADAPTIVE;
-       args.disorder = 1.0;
        i = 1;
        while (i < argc)
                arg_step(argv[i++], &args);
+       args.disorder = compute_disorder(&args.state.a);
        args.algo_func = algorithm_get(args.algo, args.disorder);
        res = pushswap_solve(&args);
        output_flush();
        printf("pre: %lu\n", res.pre_opt.counters[OP_RR]);
        printf("post: %lu\n", res.post_opt.counters[OP_RR]);
+       printf("num: %lu\n", args.disorder.num);
+       printf("den: %lu\n", args.disorder.den);
        cheatexit(!stacks_is_solved(&args.state));
 }
index f816a40cd890ca54279fa1ba304a0a2d94b2d4f4..cac3466557caf60ed6b2c6f0d06d528818e1f255 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: clefrere <clefrere@student.42.fr>          +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/12/02 11:02:44 by agilliar          #+#    #+#             */
-/*   Updated: 2025/12/18 13:46:31 by clefrere         ###   ########.fr       */
+/*   Updated: 2025/12/18 14:07:48 by agilliar         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -173,12 +173,12 @@ void                      leaf_top_b_a(const t_stacks *stacks, t_closure cb,
 void                   splitsort(const t_stacks *stacks, t_closure cb, size_t blocks);
 
 void                   algorithm_simple(const t_stacks *stacks, t_closure cb);
+void                   algorithm_medium(const t_stacks *stacks, t_closure cb);
 void                   algorithm_complex(const t_stacks *stacks, t_closure cb);
-void                   algorithm_leafsort(const t_stacks *stacks, t_closure cb);
 
 t_frac                 frac_new(size_t num, size_t den);
 t_frac                 frac_mul(t_frac lhs, t_frac rhs);
-bool                   frac_lte(t_frac lhs, t_frac rhs);
+bool                   frac_lt(t_frac lhs, t_frac rhs);
 
 t_frac                 compute_disorder(const t_stack *a);