new day, new sort =)
optimised sorting algorithm
This commit is contained in:
124
srcs/push_swap.c
124
srcs/push_swap.c
@@ -6,13 +6,82 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/03 11:41:49 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/06 16:24:16 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/03/08 18:02:31 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "push_swap.h"
|
||||
|
||||
int sort(t_psdata *data, int size)
|
||||
void mark_groups(t_psdata *data)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int inds;
|
||||
|
||||
ptr = data->a;
|
||||
inds = 1;
|
||||
while (ptr)
|
||||
{
|
||||
ptr->ind = inds;
|
||||
if (ptr->next && ptr->nb > ptr->next->nb)
|
||||
inds++;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
}
|
||||
|
||||
void sort2(t_psdata *data)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int inds;
|
||||
int inds2;
|
||||
int i;
|
||||
|
||||
ptr = data->a;
|
||||
while (ptr && ptr->next)
|
||||
ptr = ptr->next;
|
||||
inds2 = ptr->ind + 1;
|
||||
while (data->a->ind != ptr->ind)
|
||||
{
|
||||
inds = data->a->ind;
|
||||
while (data->a->ind == inds)
|
||||
{
|
||||
ft_printf("pb\n");
|
||||
ps_push(data, 'b');
|
||||
if (data->size_b == 2)
|
||||
ft_printf("sb\n");
|
||||
if (data->size_b == 2)
|
||||
ps_swap(data, 'b');
|
||||
else if (data->size_b > 2)
|
||||
ft_printf("rb\n");
|
||||
if (data->size_b > 2)
|
||||
ps_rot(data, 'b');
|
||||
}
|
||||
inds = data->a->ind;
|
||||
while (inds == data->a->ind || data->size_b > 0)
|
||||
{
|
||||
if (data->a->ind != inds || (data->size_b > 0 && data->b->nb < data->a->nb))
|
||||
{
|
||||
ft_printf("pa\n");
|
||||
ps_push(data, 'a');
|
||||
}
|
||||
data->a->ind = inds2;
|
||||
ft_printf("ra\n");
|
||||
ps_rot(data, 'a');
|
||||
}
|
||||
i = 0;
|
||||
ptr = data->a;
|
||||
while (ptr)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
i++;
|
||||
}
|
||||
ptr = data->a;
|
||||
while (ptr && ptr->next)
|
||||
ptr = ptr->next;
|
||||
inds2 = ptr->ind + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int sort(t_psdata *data, int size)
|
||||
{
|
||||
t_stack *ptr;
|
||||
int i;
|
||||
@@ -39,14 +108,16 @@ int sort(t_psdata *data, int size)
|
||||
}
|
||||
i = 0;
|
||||
ptr = data->a;
|
||||
while (i < ((size % 2) ? size + 1 : size) / 2)
|
||||
while ((i < ((size % 2) ? size + 1 : size) / 2) || (ptr->next != NULL && ptr->nb < ptr->next->nb))
|
||||
{
|
||||
ptr->ind = 1;
|
||||
ptr = ptr->next;
|
||||
i++;
|
||||
if (ptr->next)
|
||||
return (1);
|
||||
}
|
||||
i = 0;
|
||||
while (ptr && i < (size - (((size % 2) ? size + 1 : size) / 2)))
|
||||
while ((ptr && i < (size - (((size % 2) ? size + 1 : size) / 2)))/* || (ptr != NULL && ptr->next != NULL && ptr->nb < ptr->next->nb)*/)
|
||||
{
|
||||
ptr->ind = 2;
|
||||
ptr = ptr->next;
|
||||
@@ -95,28 +166,35 @@ int main(int ac, char **av)
|
||||
ptr->ind = 0;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
if (!sort(&data, data.size_a))
|
||||
mark_groups(&data);
|
||||
ptr = data.a;
|
||||
i = 0;
|
||||
while (ptr)
|
||||
{
|
||||
printf("Error\n");
|
||||
ps_stkclean(&data);
|
||||
return (1);
|
||||
// ft_printf("@ind: %d nb: %d ind: %d\n", i, ptr->nb, ptr->ind);
|
||||
ptr = ptr->next;
|
||||
i++;
|
||||
}
|
||||
/* ptr = data.a; */
|
||||
/* i = data.a->nb; */
|
||||
/* while (ptr) */
|
||||
sort2(&data);
|
||||
/* if (!sort(&data, data.size_a)) */
|
||||
/* { */
|
||||
/* printf("%d\n", ptr->nb); */
|
||||
/* ptr->ind = 0; */
|
||||
/* if (i != ptr->nb && i > ptr->nb) */
|
||||
/* { */
|
||||
/* printf("KO\n"); */
|
||||
/* break ; */
|
||||
/* } */
|
||||
/* i = ptr->nb; */
|
||||
/* ptr = ptr->next; */
|
||||
/* printf("Error\n"); */
|
||||
/* ps_stkclean(&data); */
|
||||
/* return (1); */
|
||||
/* } */
|
||||
/* if (ptr == NULL) */
|
||||
/* printf("OK\n"); */
|
||||
/* ps_stkclean(&data); */
|
||||
ptr = data.a;
|
||||
while (ptr)
|
||||
{
|
||||
// ft_printf("%d\n", ptr->nb);
|
||||
if (ptr->next && ptr->nb > ptr->next->nb)
|
||||
{
|
||||
// ft_printf("KO\n");
|
||||
ps_stkclean(&data);
|
||||
return (0);
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
// ft_printf("OK\n");
|
||||
ps_stkclean(&data);
|
||||
return (0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user