This commit is contained in:
kinou-p 2022-01-15 23:49:27 +01:00
parent 5b48138108
commit 597e1834e3
13 changed files with 403 additions and 208 deletions

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2021/11/13 13:06:47 by apommier #+# #+# # # Created: 2021/11/13 13:06:47 by apommier #+# #+# #
# Updated: 2022/01/08 18:27:15 by apommier ### ########.fr # # Updated: 2022/01/14 20:19:54 by apommier ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -15,9 +15,11 @@ SRCS = push_swap.c \
process.c\ process.c\
main.c\ main.c\
sorting.c\ sorting.c\
s_list.c\
set_a.c\
indexing.c indexing.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
CFLAGS = CFLAGS =
RM = rm -rf RM = rm -rf
LIBFT = ./libft LIBFT = ./libft

View File

@ -35,7 +35,7 @@ SRCS = ft_memset.c \
ft_putnbr_fd.c ft_putnbr_fd.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
BONUS_C = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back.c \ BONUS_C = ft_lstnew.c ft_lstadd_front.c ft_lstsize.c ft_lstlast.c ft_lstadd_back.c \
ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c ft_lstdelone.c ft_lstclear.c ft_lstiter.c ft_lstmap.c ft_lstbeforelast.c
BONUS_O = ${BONUS_C:.c=.o} BONUS_O = ${BONUS_C:.c=.o}
CC = gcc CC = gcc
CFLAGS = -Wall -Wextra -Werror CFLAGS = -Wall -Wextra -Werror

View File

@ -1,13 +1,28 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* sort5.c :+: :+: :+: */ /* ft_lstbeforelast.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:15:27 by apommier #+# #+# */ /* Created: 2021/12/09 19:16:55 by apommier #+# #+# */
/* Updated: 2021/11/25 23:15:27 by apommier ### ########.fr */ /* Updated: 2022/01/13 19:00:16 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "push_swap.h" #include "libft.h"
t_list *ft_lstbeforelast(t_list *lst)
{
t_list *save;
save = 0;
if (!lst)
return (0);
while (lst->next)
{
save = lst;
lst = lst->next;
}
return (save);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/09 01:06:20 by apommier #+# #+# */ /* Created: 2020/12/09 01:06:20 by apommier #+# #+# */
/* Updated: 2022/01/06 01:35:14 by apommier ### ########.fr */ /* Updated: 2022/01/15 23:45:37 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,6 +19,7 @@ t_list *ft_lstnew(void *content)
new = (t_list*)malloc(sizeof(t_list)); new = (t_list*)malloc(sizeof(t_list));
if (!new) if (!new)
return (0); return (0);
new->swap = 0;
new->nbr = content; new->nbr = content;
new->next = 0; new->next = 0;
return (new); return (new);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/11 03:21:27 by apommier #+# #+# */ /* Created: 2020/12/11 03:21:27 by apommier #+# #+# */
/* Updated: 2022/01/09 18:49:47 by apommier ### ########.fr */ /* Updated: 2022/01/13 18:56:56 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -70,5 +70,6 @@ void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *)); void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), t_list *ft_lstmap(t_list *lst, void *(*f)(void *),
void (*del)(void *)); void (*del)(void *));
t_list *ft_lstbeforelast(t_list *lst);
#endif #endif

5
main.c
View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:15:43 by apommier #+# #+# */ /* Created: 2021/11/25 23:15:43 by apommier #+# #+# */
/* Updated: 2022/01/08 22:12:05 by apommier ### ########.fr */ /* Updated: 2022/01/15 20:51:55 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,9 +19,6 @@ int main(int argc, char *argv[])
ft_putstr_fd("Error\n", 2); ft_putstr_fd("Error\n", 2);
return (1); return (1);
} }
if (argc == 2)
return (1);
push_swap(argc, argv); push_swap(argc, argv);
printf("------end------\n");
return (1); return (1);
} }

View File

@ -22,10 +22,10 @@ t_list *ft_sa_sb(t_list **list, s_list **process, char type, int set)
(*list)->next->next = swap; (*list)->next->next = swap;
if (set) if (set)
{ {
if (type = 'a') if (type == 'a')
s_lstadd_back(process, new_slist("sa")); s_lstadd_back(process, new_slist("sa\n"));
else if (type = 'b') else if (type == 'b')
s_lstadd_back(process, new_slist("sb")); s_lstadd_back(process, new_slist("sb\n"));
} }
} }
@ -37,10 +37,10 @@ t_list *ft_ra_rb(t_list **list, s_list **process, char type)
(ft_lstlast(*list))->next = *list; (ft_lstlast(*list))->next = *list;
*list = (*list)->next; *list = (*list)->next;
swap->next = 0; swap->next = 0;
if (type = 'a') if (type == 'a')
s_lstadd_back(process, new_slist("ra")); s_lstadd_back(process, new_slist("ra\n"));
else if (type = 'b') else if (type == 'b')
s_lstadd_back(process, new_slist("rb")); s_lstadd_back(process, new_slist("rb\n"));
} }
t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process) t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process)
@ -50,7 +50,8 @@ t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process)
swap = *list_b; swap = *list_b;
*list_b = (*list_b)->next; *list_b = (*list_b)->next;
ft_lstadd_front(list_a, swap); ft_lstadd_front(list_a, swap);
s_lstadd_back(process, new_slist("pa")); s_lstadd_back(process, new_slist("pa\n"));
return (*list_b);
} }
t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process) t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process)
@ -60,12 +61,18 @@ t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process)
swap = *list_a; swap = *list_a;
*list_a = (*list_a)->next; *list_a = (*list_a)->next;
ft_lstadd_front(list_b, swap); ft_lstadd_front(list_b, swap);
s_lstadd_back(process, new_slist("pb")); s_lstadd_back(process, new_slist("pb\n"));
} }
t_list * ft_rra_rrb(t_list **list, s_list **process, char type) t_list * ft_rra_rrb(t_list **list, s_list **process, char type)
{ {
t_list *swap; t_list *swap;
swap = *list; swap = ft_lstlast(*list);
ft_lstbeforelast(*list)->next = 0;
ft_lstadd_front(list, swap);
if (type == 'a')
s_lstadd_back(process, new_slist("rra\n"));
else if (type == 'b')
s_lstadd_back(process, new_slist("rrb\n"));
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:10:37 by apommier #+# #+# */ /* Created: 2021/11/25 23:10:37 by apommier #+# #+# */
/* Updated: 2022/01/08 18:25:27 by apommier ### ########.fr */ /* Updated: 2022/01/15 23:28:47 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,32 +23,35 @@ void printf_list(t_list *start)
printf("lst%d --- nbr: %d index: %d\n", i,*(int*)start->nbr, start->index); printf("lst%d --- nbr: %d index: %d\n", i,*(int*)start->nbr, start->index);
start = start->next; start = start->next;
} }
printf("----end_print----"); printf("----end_print----\n");
return; return;
} }
int is_nbr(int nbrarg, char **list) int is_nbr(int nbrarg, char **list)
{ {
int i; int i;
int j; int j;
i = 0; i = 0;
j = 0; j = 0;
while (++j < nbrarg) while (--nbrarg)
{ {
j++;
i = 0; i = 0;
if (list[j][i] == '-' && !list[j][i++])
return (0);
while (list[j][i]) while (list[j][i])
{ {
if (list[j][i] < '0' || list[j][i] > '9') while (list[j][i] == ' ')
{ i++;
if (list[j][i] == '-' || (list[j][i] == '+'))
i++;
if (list[j][i] && (list[j][i] < '0' || list[j][i] > '9'))
return (0);
while (list[j][i] && list[j][i] >= '0' && list[j][i] <= '9')
i++;
if (list[j][i] && list[j][i] != ' ')
return (0); return (0);
}
i++;
} }
} }
return (1); return (1);
} }
@ -90,19 +93,15 @@ int push_swap(int nbrarg, char **list)
return (0); return (0);
} }
start = set_list(--nbrarg, list); start = set_list(--nbrarg, list);
if (ft_lstsize(start) == 1)
return(1);
lst_indexing(start); lst_indexing(start);
printf_list(start);
find_loop_index(start); find_loop_index(start);
if (is_sorted(start)) ft_lstclear(&start, &free);
{
ft_lstclear(&start, &free);
return (1);
}
ft_lstclear(&start, &free);
return (1); return (1);
} }
t_list *set_list(int nbrarg, char **srcs) /*t_list *set_list(int nbrarg, char **srcs)
{ {
int *tmp; int *tmp;
t_list *start; t_list *start;
@ -112,37 +111,45 @@ t_list *set_list(int nbrarg, char **srcs)
swap = 0; swap = 0;
*(srcs)++; *(srcs)++;
tmp = ft_calloc(sizeof(int), 1); tmp = ft_calloc(sizeof(int), 1);
*tmp = ft_atoi(*srcs); *tmp = ft_atoi*(srcs);
start = ft_lstnew(tmp); start = ft_lstnew(tmp);
swap = start; swap = start;
while (--nbrarg) while (--nbrarg)
{ {
*(srcs)++; *(srcs)++;
tmp = ft_calloc(sizeof(int), 1); tmp = ft_calloc(sizeof(int), 1);
*tmp = ft_atoi(*srcs); *tmp = ft_atoi*(srcs);
swap->next = ft_lstnew(tmp); swap->next = ft_lstnew(tmp);
swap = swap->next; swap = swap->next;
} }
return (start); return (start);
} }*/
int is_sorted(t_list *list) t_list *set_list(int nbrarg, char **srcs)
{ {
while (list->next) int *tmp;
int i;
int j;
t_list *swap;
j = 1;
tmp = 0;
i = 0;
swap = 0;
while (nbrarg--)
{ {
if (*(int*)list->nbr > *(int*)list->next->nbr) i = 0;
return (0); while (srcs[j][i])
else {
list = list->next; tmp = ft_calloc(sizeof(int), 1);
*tmp = ft_atoi(&srcs[j][i]);
ft_lstadd_back(&swap, ft_lstnew(tmp));
while (srcs[j][i] == ' ')
i++;
while (srcs[j][i] && srcs[j][i] != ' ')
i++;
}
j++;
} }
return (1); return (swap);
} }
/*void choose_algo(t_list *list)
{
int size;
size = ft_lstsize(list);
if (size == 3)
sort3(list);
}*/

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:15:17 by apommier #+# #+# */ /* Created: 2021/11/25 23:15:17 by apommier #+# #+# */
/* Updated: 2022/01/09 18:51:38 by apommier ### ########.fr */ /* Updated: 2022/01/14 23:03:47 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,18 +33,27 @@ int is_sorted(t_list *list);
void find_loop_index(t_list *list); void find_loop_index(t_list *list);
int find_loop(t_list *start, t_list *list, int setswap); int find_loop(t_list *start, t_list *list, int setswap);
t_list *ft_lstnew(void *content); t_list *ft_lstnew(void *content);
void s_lstclear(s_list **lst, void (*del)(void*));
void swap_to_b(t_list *list, t_list *best, int loop);
s_list *new_slist(void *content); s_list *new_slist(void *content);
void s_lstadd_back(s_list **alst, s_list *new); void s_lstadd_back(s_list **alst, s_list *new);
s_list *s_lstlast(s_list *lst); s_list *s_lstlast(s_list *lst);
void s_lstclear(s_list **lst);
void printf_slist(s_list *start);
t_list *ft_sa_sb(t_list **list, s_list **process, char type, int set); t_list *ft_sa_sb(t_list **list, s_list **process, char type, int set);
t_list *ft_ra_rb(t_list **list, s_list **process, char type); t_list *ft_ra_rb(t_list **list, s_list **process, char type);
t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process); t_list *ft_pa(t_list **list_a, t_list **list_b, s_list **process);
t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process); t_list *ft_pb(t_list **list_a, t_list **list_b, s_list **process);
t_list *ft_rra_rrb(t_list **list, s_list **process, char type);
t_list *swap(t_list *a, t_list **b, s_list **action, t_list *to_swap);
t_list *make_list(int place, t_list *list, s_list *action, char type);
t_list *swap_to_a(t_list *a, t_list *b, t_list *start_b, s_list *action);
int find_place_for_b(t_list *list, int index, int size_b);
int find_nbr(int size_b, t_list *a, t_list *b, int index_b);
void swap_to_b(t_list *list, t_list *best, t_list *b, int loop);
t_list *turn_list(t_list *a, s_list *action);
void printf_list(t_list *start); void printf_list(t_list *start);

View File

@ -24,7 +24,7 @@ s_list *new_slist(void *content)
return (new); return (new);
} }
void s_lstclear(s_list **lst, void (*del)(void*)) void s_lstclear(s_list **lst)
{ {
s_list *chr; s_list *chr;
@ -32,7 +32,6 @@ void s_lstclear(s_list **lst, void (*del)(void*))
while (*lst) while (*lst)
{ {
chr = (*lst)->next; chr = (*lst)->next;
del((*lst)->action);
free(*lst); free(*lst);
*lst = chr; *lst = chr;
} }
@ -44,7 +43,8 @@ void s_lstadd_back(s_list **alst, s_list *new)
if (*alst == 0) if (*alst == 0)
*alst = new; *alst = new;
else else
ft_lstlast(*alst)->next = new; s_lstlast(*alst)->next = new;
new->next = 0;
} }
s_list *s_lstlast(s_list *lst) s_list *s_lstlast(s_list *lst)
@ -52,6 +52,21 @@ s_list *s_lstlast(s_list *lst)
if (!lst) if (!lst)
return (0); return (0);
while (lst->next) while (lst->next)
{
//printf("test lst = %s\n", (char*)lst->action);
lst = lst->next; lst = lst->next;
}
return (lst); return (lst);
} }
void printf_slist(s_list *start)
{
int i = 0;
while(start)
{
i++;
ft_putstr_fd((char*)start->action, 1);
start = start->next;
}
return;
}

129
set_a.c Normal file
View File

@ -0,0 +1,129 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set_a.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/07 19:14:03 by apommier #+# #+# */
/* Updated: 2022/01/15 23:30:15 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void find_loop_index(t_list *list)
{
t_list *start;
t_list *save;
int loop_size;
int loop_size2;
loop_size2 = 0;
loop_size = 0;
start = list;
while (list)
{
loop_size = find_loop(start, list, 0);
if (loop_size > loop_size2)
{
loop_size2 = loop_size;
save = list;
}
list = list->next;
}
find_loop(start, save, 1);
swap_to_b(start, save, 0, loop_size2);
}
int find_loop(t_list *start, t_list *list, int setswap)
{
int countdown;
int loop_size;
t_list *swap;
loop_size = 0;
swap = list->next;
countdown = ft_lstsize(start) - 1;
while (countdown)
{
if (swap)
{
if (swap->index > list->index)
{
list = swap;
loop_size++;
}
else if (setswap)
swap->swap = 1;
countdown--;
swap = swap->next;
}
else
swap = start;
}
return (loop_size);
}
void swap_to_b(t_list *list, t_list *best, t_list *b, int loop)
{
int swap;
int lst_size;
s_list *action;
action = 0;
lst_size = ft_lstsize(list);
swap = 0;
while (lst_size)
{
ft_sa_sb(&list, &action, 'a', 0);
swap = find_loop(list, best, 0);
if (swap > loop)
{
loop = swap;
s_lstadd_back(&action, new_slist("sa\n"));
}
else
ft_sa_sb(&list, &action, 'a', 0);
if (list->swap)
ft_pb(&list, &b, &action);
else
ft_ra_rb(&list, &action, 'a');
lst_size--;
}
list = swap_to_a(list, b, b, action);
list = turn_list(list, action);
printf_slist(action);
s_lstclear(&action);
}
t_list *turn_list(t_list *a, s_list *action)
{
t_list *start;
int place;
start = a;
place = 0;
while (a->index > 1)
{
a = a->next;
place++;
}
a = start;
//printf("place = %d size = %d\n", place, ft_lstsize(a));
if (place > ft_lstsize(a) / 2 && place > 2)
{
//printf("if\n");
while (ft_lstsize(start) - (place++))
ft_rra_rrb(&start, &action, 'a');
}
else
{
//printf("else\n");
while (place--)
ft_ra_rb(&start, &action, 'a');
}
//printf_list(start);
return (start);
}

41
sort3.c
View File

@ -1,41 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sort3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/25 23:15:22 by apommier #+# #+# */
/* Updated: 2021/11/25 23:15:22 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void sort3(t_list *list)
{
if (list->nbr < list->next->nbr && list->nbr < list->next->next->nbr)
{
ft_sa_sb(&list, process, 'a');
ft_ra_rb(&list, process, 'a');
}
if (list->nbr > list->next->nbr && list->nbr > list->next->next->nbr)
{
if (list->next->nbr > list->next->next->nbr)
{
ft_sa_sb(&list, process, 'a');
ft_ra_rb(&list, process, 'a');
ft_ra_rb(&list, process, 'a');
}
else
ft_ra_rb(&list, process, 'a');
}
if (list->nbr > list->next->nbr && list->nbr < list->next->next->nbr)
ft_sa_sb(&list, process, 'a');
if (list->nbr < list->next->nbr && list->next->next->nbr)
{
ft_ra_rb(&list, process, 'a');
ft_ra_rb(&list, process, 'a');
}
return ;
}

223
sorting.c
View File

@ -12,108 +12,161 @@
#include "push_swap.h" #include "push_swap.h"
t_list *swap(t_list *a, t_list **b, s_list **action, t_list *to_swap)
void find_loop_index(t_list *list)
{ {
t_list *start; int rotate;
t_list *save; int place;
int loop_size; t_list *start_b;
int loop_size2;
start_b = *b;
place = 0;
rotate = 0;
while ((*b)->index != to_swap->index)
{
place++;
*b = (*b)->next;
}
start_b = make_list(place, start_b, *action, 'b');
a = make_list(find_place_for_b(a, to_swap->index, ft_lstsize(start_b)), a, *action, 'a');
*b = ft_pa(&a, &start_b, action);
return (a);
}
t_list *make_list(int place, t_list *list, s_list *action, char type)
{
int lstsize;
lstsize = ft_lstsize(list);
if (place > lstsize / 2)
{
while (lstsize - place)
{
ft_rra_rrb(&list, &action, type);
place++;
}
}
else
{
while (place)
{
ft_ra_rb(&list, &action, type);
place--;
}
}
return (list);
}
t_list *swap_to_a(t_list *a, t_list *b, t_list *start_b, s_list *action)
{
int lst_size;
int size_b;
int move;
int best_move;
t_list *best_b;
int i = 0;
best_b = 0;
size_b = ft_lstsize(b);
move = size_b + ft_lstsize(a);
best_move = move;
lst_size = size_b;
while (lst_size)
{
best_move = ft_lstsize(a) + ft_lstsize(b);
b = start_b;
while (b)
{
move = find_nbr(ft_lstsize(start_b), a, start_b, b->index);
if (move < best_move)
{
best_move = move;
best_b = b;
}
b = b->next;
}
a = swap(a, &start_b, &action, best_b);
lst_size--;
}
return (a);
}
int find_place_for_b(t_list *list, int index, int size_b)
{
int count;
int save_index;
t_list *start;
loop_size2 = 0;
loop_size = 0;
start = list; start = list;
save_index = ft_lstsize(list) + size_b;
count = 0;
while (list) while (list)
{ {
loop_size = find_loop(start, list, 0); if (list->index > index && list->index < save_index)
if (loop_size > loop_size2) save_index = list->index;
{
loop_size2 = loop_size;
save = list;
}
list = list->next; list = list->next;
} }
find_loop(start, save, 1); while (start->index != save_index)
swap_to_b(start, save, loop_size2); {
printf("loop_size = %d\n", loop_size2); count++;
start = start->next;
}
return (count);
} }
int find_loop(t_list *start, t_list *list, int setswap) int find_nbr(int size_b, t_list *a, t_list *b, int index_b)
{ {
int countdown; int move;
int loop_size; int place;
t_list *swap; int size_a;
loop_size = 0; size_a = ft_lstsize(a);
swap = list->next; place = 1;
countdown = ft_lstsize(start) - 1; move = 0;
while (countdown) while (b->index != index_b)
{ {
if (swap) place++;
{ b = b->next;
if (list->index == swap->index - 1 - loop_size)
loop_size++;
else if (setswap)
swap->swap = 1;
countdown--;
swap = swap->next;
}
else
swap = start;
} }
printf("------------return= %d\n", loop_size); move = find_place_for_b(a, b->index, size_b);
return (loop_size); if (move > size_a / 2)
} move = size_a - move;
if (place > size_b / 2)
move += size_b - place;
else
move += place;
return (move);
}
void swap_to_b(t_list *list, t_list *best, int loop) /*void choose_swap(t_list *a, t_list *b, s_list *action, int lst_size)
{ {
s_list *action; int size_a;
int swap; int size_b;
int lst_size; t_list *save_b;
t_list *b; int move;
int save_move;
b = 0; size_b = lst_size;
action = 0; size_a = ft_lstsize(a);
lst_size = ft_lstsize(list); printf("swapto a\n");
swap = 0; move = 0;
printf_list(list); save_move = size_b + size_a;
while (lst_size) while (lst_size)
{ {
ft_sa_sb(&list, &action, 'a', 0); move = find_place_for_b(a, b->index);
swap = find_loop(list, best, 0); if (move > size_a / 2)
if (swap > loop) move = size_a - move;
{ if (lst_size < size_b / 2)
loop = swap; move += size_b - lst_size;
s_lstadd_back(&action, new_slist("sa"));
}
else else
ft_sa_sb(&list, &action, 'a', 0); move += lst_size;
if(list->swap) if (move < save_move)
ft_pb(&list, &b, &action); {
else if (!list->swap) save_move = move;
ft_ra_rb(&list, &action, 'a'); save_b = b;
}
b = b->next;
lst_size--; lst_size--;
printf_list(list);
} }
printf("\nswap done\n"); find_nbr();
} }*/
/*while (countdown - 1)
{
if (swap)
{
if (list->index == swap->index - 1 - loop_size)
{
printf("swap= %d index= %d so ++++++\n", *(int*)swap->nbr, swap->index);
loop_size++;
}
else
printf("nop swap = %d index = %d\n", *(int*)swap->nbr, swap->index);
countdown--;
swap = swap->next;
}
else
swap = start;
}*/