Exemplo n.º 1
0
static int		ft_algo_bis(t_ps *a, t_ps *b, t_env *env)
{
	int i;

	i = 0;
	if (a && a->next && a->rp > a->next->rp)
		i += ft_sa(env);
	else if (b == NULL)
	{
		if (env->b_pb_first == 0)
			env->b_pb_first = a->rp;
		i += ft_pb(env);
	}
	else if (a->rp > b->rp)
		i += ft_pb(env);
	else if (a->rp < b->rp && a->rp != env->stop && ft_lstlen(env->a_first) > 3)
	{
		if (env->stop == 0)
			env->stop = a->rp;
		i += ft_ra(env);
	}
	else
		i += ft_mix_ab(env);
	return (i);
}
Exemplo n.º 2
0
int				ft_f(int f, t_ps *ps)
{
	if (f == SA)
		return (ft_sa(ps));
	else if (f == SB)
		return (ft_sb(ps));
	else if (f == SS)
		return (ft_ss(ps));
	else if (f == PB)
		return (ft_pb(ps));
	else if (f == PA)
		return (ft_pa(ps));
	else if (f == RA)
		return (ft_ra(ps));
	else if (f == RB)
		return (ft_rb(ps));
	else if (f == RR)
		return (ft_rr(ps));
	else if (f == RRA)
		return (ft_rra(ps));
	else if (f == RRB)
		return (ft_rrb(ps));
	else if (f == RRR)
		return (ft_rrr(ps));
	return (0);
}
Exemplo n.º 3
0
void	small_swap(t_shell *h)
{
	while (l_len(h->sa) > 1)
	{
		if (l_len(h->sb) > 1 && h->sa->nb > h->sa->n->nb &&
h->sb->nb < h->sb->n->nb)
			ft_ss(h, 1);
		else if (h->sa->nb > h->sa->n->nb)
			ft_sa(h, 1);
		else if (l_len(h->sb) > 1 && h->sb->nb < h->sb->n->nb)
			ft_sb(h, 1);
		if (h->max == h->sa->nb || (l_len(h->sb) > 2 &&
h->sa->nb > h->sa->n->nb && h->sa->nb > h->sa->n->n->nb))
			ft_ra(h, 1);
		while (l_len(h->sa) > 2 &&
l_nb(h->sa) < h->sa->nb && l_nb(h->sa) < h->sa->n->nb)
			ft_rra(h, 1);
		if (l_len(h->sb) > 1 && l_nb(h->sb) > h->sb->nb)
			ft_rrb(h, 1);
		if (ft_sorted(h->sa))
			break ;
		ft_pb(h, 1);
		if (l_len(h->sb) > 2 && ((h->sb->nb < h->sb->n->nb
	&& h->sb->nb < h->sb->n->n->nb) || l_nb(h->sb) > h->sb->nb))
			ft_rb(h, 1);
	}
	push_swap_suite(h);
}
Exemplo n.º 4
0
void	push_swap(t_shell *h, int x, int len, int i)
{
	int *big;

	len = l_len(h->sa) / 2;
	big = get_biggest(h->sa, len, x);
	h->min = x == INT_MIN ? big[0] : h->min;
	i = len;
	while (i)
	{
		if (arr(h->sa->nb, big, len) && arr(h->sa->n->nb, big, len) &&
h->sa->n->nb > h->sa->nb)
			ft_sa(h, 1);
		if (arr(h->sa->nb, big, len))
		{
			ft_pb(h, 1);
			i--;
		}
		else
			ft_ra(h, 1);
		sort_b_list(h, big, len);
	}
	rempile(h);
	if (x == INT_MIN)
		push_swap(h, big[len - 1], 0, 0);
	free(big);
	finish(h);
}
Exemplo n.º 5
0
void	ft_algo20(int mid, t_nb *nb, int list_a[nb->nba], int list_b[nb->nbb])
{
	int smallnb;

	smallnb = ft_find_smallnb(nb->nba, list_a);
	if (ft_perfect(nb, list_a, 0))
		while (nb->nbb != 0)
			ft_pa(nb, list_a, list_b);
	if (ft_perfect(nb, list_a, 0))
		return ;
	if (ft_smallnb(nb->nba, list_a, 0, nb->nba - 2) == 1)
		ft_sa(nb, list_a, list_b);
	if (smallnb >= mid && ft_smallnb(nb->nba, list_a, 0, nb->nba - 1) == 0)
		while (ft_smallnb(nb->nba, list_a, 0, nb->nba - 1) == 0)
			ft_ra(nb, list_a, list_b);
	else if (smallnb < mid && ft_smallnb(nb->nba, list_a, 0, nb->nba - 1) == 0)
		while (ft_smallnb(nb->nba, list_a, 0, nb->nba - 1) == 0)
			ft_rra(nb, list_a, list_b);
	if (ft_smallnb(nb->nba, list_a, 0, nb->nba - 1) && nb->nba > 3 &&
	ft_perfect(nb, list_a, 0) == 0)
		ft_pb(nb, list_a, list_b);
	if (nb->nba <= 3)
		ft_algo3(nb, list_a, list_b);
	if (nb->nba <= 3)
		while (nb->nbb != 0)
			ft_pa(nb, list_a, list_b);
	ft_algo20(mid, nb, list_a, list_b);
}
Exemplo n.º 6
0
static int	ft_mix_ab_bis(t_ps *a, t_ps *b, t_env *env)
{
	int i;

	i = 0;
	while (a->rp < b->rp)
	{
		i += ft_rb(env);
		b = env->b_first;
	}
	if (a->rp > b->rp)
	{
		i += ft_pb(env);
		a = env->a_first;
		b = env->b_first;
	}
	return (i);
}
Exemplo n.º 7
0
void					ft_rangetab(t_push *p)
{
	unsigned int		i;

	while (ft_isrange(p->l1, p->nbl1) != -1)
	{
		i = ft_wheremin(p->l1, p->nbl1, p);
		if (i == (unsigned int)p->nbl1 - 1 && p->nb < 3)
			ft_sa(p);
		while (p->l1[p->nbl1 - 1] != p->min && ft_isrange(p->l1, p->nbl1) != -1)
		{
			if (i == (unsigned int)p->nbl1 - 2)
				ft_sa(p);
			else
				ft_ra(p);
		}
		if (ft_isrange(p->l1, p->nbl1) != -1)
			ft_pb(p);
	}
	viderl2(p);
	return ;
}
Exemplo n.º 8
0
void	ft_nba2(int middle, t_nb *nb, int list_a[nb->nba], int list_b[nb->nbb])
{
	if (nb->nba == 2)
	{
		if (list_a[nb->nba - 1] > list_a[nb->nba - 2])
			ft_sa(nb, list_a, list_b);
		return ;
	}
	while (ft_find_inf_middlenb(nb, list_a, middle) == 0)
	{
		if (list_a[nb->nba - 1] <= middle)
			ft_pb(nb, list_a, list_b);
		if (list_a[nb->nba - 1] > middle)
			ft_ra(nb, list_a, list_b);
	}
	nb->nbinj[nb->i] = nb->nba;
	if (nb->nbinj[nb->i] < 0)
		nb->nbinj[nb->i] = 0;
	if (nb->i == 1)
		ft_nba4(ft_find_middlenb(nb, list_b, nb->nbb), nb, list_a, list_b);
	nb->i++;
	ft_nba2(ft_find_middlenb(nb, list_a, nb->nba - 1), nb, list_a, list_b);
}
Exemplo n.º 9
0
void	ft_nba4(int middle, t_nb *nb, int list_a[nb->nba], int list_b[nb->nbb])
{
	int compt;

	compt = middle / 2;
	while (nb->nba == nb->nbinj[nb->i])
	{
		while (nb->nbb != nb->nbinj[0] - nb->nbinj[nb->i - 1])
			ft_pa(nb, list_a, list_b);
		while (ft_find_inf_middlenb(nb, list_a, middle) == 0)
		{
			if (list_a[nb->nba - 1] <= middle)
				ft_pb(nb, list_a, list_b);
			if (list_a[nb->nba - 1] > middle)
				ft_ra(nb, list_a, list_b);
		}
		middle = middle + compt;
		if ((compt / 2) % 2 == 0)
			compt = compt / 2;
		else
			compt = (compt + 1) / 2;
	}
	return ;
}