new day, new sort =)
optimised sorting algorithm
This commit is contained in:
parent
3fc361618c
commit
efab9452b1
2
libft
2
libft
@ -1 +1 @@
|
||||
Subproject commit e6970465e98cdfdc3abd999e29121da8a736686e
|
||||
Subproject commit d49f9640459ae0351636e1d8da41e12264f2b518
|
@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/23 13:35:15 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/02 18:28:08 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/03/08 16:48:26 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -44,14 +44,14 @@ void exec_actions(t_psdata *data)
|
||||
int read_ops(t_psdata *data)
|
||||
{
|
||||
t_list *nop;
|
||||
char buff[5];
|
||||
char *buff;
|
||||
int ret;
|
||||
|
||||
ft_memset(buff, '\0', 5);
|
||||
while ((ret = read(1, buff, 4)) > 0)
|
||||
buff = NULL;
|
||||
while ((ret = get_next_line(0, &buff)) > 0)
|
||||
{
|
||||
buff[ft_strlen(buff) - 1] = '\0';
|
||||
if (ret > 4 || !is_op(buff) || (nop = ft_lstnew(buff, 4)) == NULL)
|
||||
ft_printf("'%s'\n", buff);
|
||||
if (ft_strlen(buff) > 4 || !is_op(buff) || (nop = ft_lstnew(buff, 4)) == NULL)
|
||||
{
|
||||
ft_putendl_fd("Error", 2);
|
||||
return (1);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/02/25 12:44:08 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/03 13:59:43 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/03/08 16:25:38 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -51,6 +51,7 @@ int get_params(t_psdata *data, int ac, char **av)
|
||||
while (tab[j])
|
||||
j++;
|
||||
while (--j >= 0)
|
||||
{
|
||||
if (check_input(tab[j], &nb) && (new = ps_stknew(nb)) != NULL)
|
||||
ps_stkpsh(data, 'a', new);
|
||||
else
|
||||
@ -59,6 +60,7 @@ int get_params(t_psdata *data, int ac, char **av)
|
||||
ps_stkclean(data);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
ft_del_words_tables(&tab);
|
||||
}
|
||||
return (1);
|
||||
|
122
srcs/push_swap.c
122
srcs/push_swap.c
@ -6,12 +6,81 @@
|
||||
/* 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"
|
||||
|
||||
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;
|
||||
@ -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; */
|
||||
/* } */
|
||||
/* if (ptr == NULL) */
|
||||
/* printf("OK\n"); */
|
||||
/* printf("Error\n"); */
|
||||
/* ps_stkclean(&data); */
|
||||
/* return (1); */
|
||||
/* } */
|
||||
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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user