This commit is contained in:
hsv2 2022-09-30 21:47:31 +02:00
parent 603d808d0e
commit 73f49eb95f
1 changed files with 29 additions and 39 deletions

View File

@ -11,65 +11,55 @@
/* ************************************************************************** */
#include "push_swap_ctx.h"
#include <limits.h>
#include <stdlib.h>
typedef struct s_range
{
size_t start;
size_t end;
size_t step;
} t_range;
static void direct_sort(t_push_swap_ctx *ctx);
static t_range init_range(const t_stack *s);
static void move_item(t_push_swap_ctx *ctx, t_stack *s,
size_t src, size_t dst);
static void range_presort(t_push_swap_ctx *ctx, int range);
int push_swap_sort(t_push_swap_ctx *ctx)
{
t_range range;
ctx->sinfo = stack_info_new(ctx->stack_a);
if (!ctx->sinfo)
return (-1);
range = init_range(ctx->stack_a);
while (!stack_int_issorted(ctx->stack_a))
{
if (range.step < 2)
{
direct_sort(ctx);
}
}
range_presort(ctx, 4);
ctx->sinfo = stack_info_del(ctx->sinfo);
return (0);
}
static void direct_sort(t_push_swap_ctx *ctx)
static void move_item(t_push_swap_ctx *ctx, t_stack *s, size_t src, size_t dst)
{
t_stackitem_info *item;
size_t rank;
size_t i;
rank = 0;
while (rank < stack_maxheight(ctx->stack_a))
stack_info_update(ctx->sinfo, 0);
item = stack_info_getitem_bypos(ctx->sinfo, src);
if (item->pos > dst)
{
stack_info_update(ctx->sinfo, 0);
item = stack_info_getitem_bytarget(ctx->sinfo, rank);
i = 0;
while (i++ < item->pos)
push_swap_exec(ctx, ps_op_rra);
push_swap_exec(ctx, ps_op_pb);
rank++;
while (item->pos-- > dst)
{
if (s == ctx->stack_a)
push_swap_exec(ctx, ps_op_rra);
else
push_swap_exec(ctx, ps_op_rrb);
}
}
else
{
while (item->pos++ < dst)
{
if (s == ctx->stack_a)
push_swap_exec(ctx, ps_op_rra);
else
push_swap_exec(ctx, ps_op_rrb);
}
}
while (rank-- > 0)
push_swap_exec(ctx, ps_op_pa);
}
static t_range init_range(const t_stack *s)
static void range_presort(t_push_swap_ctx *ctx, int r)
{
t_range r;
t_stackitem_info *item;
r.start = 0;
r.step = stack_height(s) / 10;
r.end = r.step;
return (r);
stack_info_update(ctx->sinfo, 0);
}
// vi: noet sw=4 ts=4: