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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* 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);
}
/* 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 */
/* */
/* ************************************************************************** */
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);
}
/* 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 */
/* */
/* ************************************************************************** */
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);
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);
}
#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));
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));
}
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)
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));
}
/* 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 */
/* */
/* ************************************************************************** */
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);