diff --git a/builds/algo.o b/builds/algo.o index 1633b72..cda35ae 100644 Binary files a/builds/algo.o and b/builds/algo.o differ diff --git a/builds/push_swap.o b/builds/push_swap.o index 69d98a8..6cf1d4c 100644 Binary files a/builds/push_swap.o and b/builds/push_swap.o differ diff --git a/checker b/checker new file mode 100755 index 0000000..002d593 Binary files /dev/null and b/checker differ diff --git a/push_swap b/push_swap index 6e39fc1..9455432 100755 Binary files a/push_swap and b/push_swap differ diff --git a/push_swap_tester b/push_swap_tester new file mode 160000 index 0000000..0fd5c41 --- /dev/null +++ b/push_swap_tester @@ -0,0 +1 @@ +Subproject commit 0fd5c414c6de12e679f6a0d19bc5ad00de38215f diff --git a/srcs/algo.c b/srcs/algo.c index 6b8229b..de2cd12 100644 --- a/srcs/algo.c +++ b/srcs/algo.c @@ -126,26 +126,202 @@ void send_sublist_to_a(t_stack **a, t_stack **b, t_list *solution) mark_step(solution, "send_sublist_to_a"); } +/* +int find_smallest(t_stack *list, int size) +{ + t_stack *head; + int smallest; + int position; + + smallest = list->n; + head = list; + while (--size > 0) + { + head = head->next; + if (head->n < smallest) + smallest = head->n; + } + position = 0; + while (list->n != smallest) + { + position++; + list = list->next; + } + return (position); +} + void mini_sort(t_stack **a, t_stack **b, t_list *solution) { - (void)a; - (void)b; - (void)solution; + int list_size; + int next; + int temp; + + list_size = sublist_size(*a); + if(list_size == 1) + return ; + (*a)->limit = 0; + while (--list_size > 0) + { + next = find_smallest(*a, list_size + 1); + temp = next; + while (next-- > 0) + ra(a, &solution); + pb(b, a, &solution); + while (temp-- > 0) + rra(a, &solution); + } + list_size = sublist_size(*b); + while (list_size-- > 0) + pa(a, b, &solution); + (*a)->limit = 1; + mark_step(solution, "mini_sort"); +} +*/ + +/* +void check_front(t_stack *list, int i, int *position, int *smallest) +{ + int j; + + j = 1; + while (list->next && j++ < i) + list = list->next; + if (list->n < *smallest) + { + *smallest = list->n; + *position = i; + } +} + +void check_back(t_stack *list, int i, int *position, int *smallest) +{ + t_stack *head; + int j; + + head = list; + j = 1; + while (head != NULL) + { + head = head->next; + j++; + } + head = list; + j -= i; + while (head->next && j-- >= 0) + head = head->next; + if (head->n < *smallest) + { + *smallest = head->n; + *position = i; + } +} + +int find_smallest(t_stack *list, int size) +{ + int i; + int front; + int back; + int smallest; + int position; + + smallest = list->n; + i = 1; + position = 1; + front = sublist_size(list); + back = size - front; + while (++i <= front) + check_front(list, i, &position, &smallest); + i = 0; + while (--i >= back) + check_back(list, i, &position, &smallest); +ft_printf(" / smallest:%i ", smallest); + return (position); +} +*/ + +void mark_sublist(t_stack *list) +{ + while (list && list->limit != 1) + { + if (list->limit == 2) + list->limit = 0; + else if (list->limit == 0) + list->limit = 2; + list = list->next; + } +} + +int find_smallest(t_stack *list) +{ + int i; + int smallest; + int position; + + smallest = list->n; + position = 1; + i = 0; + while (list) + { + i++; + if (list->limit == 2 && list->n < smallest) + { + smallest = list->n; + position = i; + } + list = list->next; + } + if (i - position + 1 < position) + position = position - i - 1; + return (position); +} + +void mini_sort(t_stack **a, t_stack **b, t_list *solution) +{ + int list_size; + int next; + int i; + + list_size = sublist_size(*a); + if(list_size == 1) + return ; + (*a)->limit = 0; + mark_sublist(*a); +t_stack *test;test = *a;while (test){ft_printf("%i(%i)-", test->n, test->limit);test = test->next;} + while (--list_size >= 0) + { + i = 0; + next = find_smallest(*a); +ft_printf(" / %i", next); + if (next > 0) + while (++i < next) + ra(a, &solution); + else if (next < 0) + while (i-- > next) + rra(a, &solution); + pb(b, a, &solution); + } + list_size = sublist_size(*b); + while (list_size-- > 0) + pa(a, b, &solution); + mark_sublist(*a); + (*a)->limit = 1; + mark_step(solution, "mini_sort"); } void hugo_sort(t_stack **a, t_stack **b, t_list *solution) { if (sublist_size(*a) > 5) divide_a(a, b, solution); - else if (sublist_size(*b) > 5) + else { mini_sort(a, b, solution); - divide_b(a, b, solution); + if (sublist_size(*b) > 5) + divide_b(a, b, solution); + else if (sublist_size(*b) > 0) + send_sublist_to_a(a, b, solution); + else + return ; } - else if (sublist_size(*b) > 0) - send_sublist_to_a(a, b, solution); - else - return ; hugo_sort(a, b, solution); } diff --git a/srcs/push_swap.c b/srcs/push_swap.c index 4cdeaef..79d8810 100644 --- a/srcs/push_swap.c +++ b/srcs/push_swap.c @@ -25,23 +25,40 @@ int check_flag(int *ac, char ***av) return (0); } +t_stack *fill_stack(t_stack *list, char *nb) +{ + t_stack *new; + + new = ft_calloc(1, sizeof(t_stack)); + if (!new) + ps_stop(NULL, NULL, 2); + new->n = ft_atoi(nb); + new->limit = 0; + new->next = list; + list = new; + return (list); +} + t_stack *init_stack(int ac, char **av) { - t_stack *start; - t_stack *tmp; + char **split; + t_stack *list; + int i; - tmp = NULL; - while (--ac) + list = NULL; + if (ac > 2) + while (--ac) + list = fill_stack(list, av[ac]); + else if (ac == 2) { - start = ft_calloc(1, sizeof(t_stack)); - if (!start) - ps_stop(NULL, NULL, 2); - start->n = ft_atoi(av[ac]); - start->limit = 0; - start->next = tmp; - tmp = start; + split = ft_split(av[1], ' '); + i = 0; + while (split[i] != NULL) + i++; + while (--i >= 0) + list = fill_stack(list, split[i]); } - return (start); + return (list); } /*