From: = <=> Date: Thu, 18 Dec 2025 13:14:40 +0000 (+0100) Subject: Medium algorithm X-Git-Url: https://git.uwuaxy.net/?a=commitdiff_plain;h=164a29488cca0bb10339d35d898d70a4c89554e3;p=axy%2Fft%2Fpushswap.git Medium algorithm --- diff --git a/Makefile b/Makefile index 831306a..ee8dee5 100644 --- 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 diff --git a/algorithm_leafsort.c b/algorithm_medium.c similarity index 57% rename from algorithm_leafsort.c rename to algorithm_medium.c index 6e36239..aa9aabc 100644 --- a/algorithm_leafsort.c +++ b/algorithm_medium.c @@ -1,18 +1,28 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* algorithm_leafsort.c :+: :+: :+: */ +/* algorithm_medium.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: agilliar +#+ +:+ +#+ */ +/* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* 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 449a18d..2a1addd 100644 --- a/frac.c +++ b/frac.c @@ -6,7 +6,7 @@ /* By: agilliar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); } diff --git a/main_pushswap.c b/main_pushswap.c index 8aee612..e46902f 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/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)); } diff --git a/pushswap.h b/pushswap.h index f816a40..cac3466 100644 --- a/pushswap.h +++ b/pushswap.h @@ -6,7 +6,7 @@ /* By: clefrere +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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);