Disable using LIS for very small input
authorLukas Jiriste <ljiriste@student.42prague.com>
Tue, 5 Mar 2024 10:57:32 +0000 (11:57 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Tue, 5 Mar 2024 10:57:32 +0000 (11:57 +0100)
When sorting lists smaller than 7 elements, the LIS
algorithm performes worse than the push-only one.
This is probably caused by additional rotations, that are
needed to isolate LIS in stack A.

src/push.c

index 909bffd8cd23be4a1fe36589813f9dc5e9b11e05..ac06c0bafe6cdf06ca15cf89425a3e575e67cfea 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/01/24 13:17:32 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/03/05 09:40:03 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/03/05 11:54:36 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -43,6 +43,13 @@ void get_push_indeces(t_stacks *s, int *ind_a, int *ind_b)
        return ;
 }
 
+void   push_all_but_three(t_stacks *s)
+{
+       while (s->a.stack.size > 3)
+               print_push(s, b);
+       return ;
+}
+
 void   sort(t_stacks *s)
 {
        int             ind_a;
@@ -50,9 +57,14 @@ void sort(t_stacks *s)
        t_stack lis;
 
        init_stack(&lis);
-       get_circular_lis(&lis, &s->a);
-       isolate_lis_in_a(s, &lis);
-       free_stack(&lis);
+       if (s->a.stack.size > 6)
+       {
+               get_circular_lis(&lis, &s->a);
+               isolate_lis_in_a(s, &lis);
+               free_stack(&lis);
+       }
+       else
+               push_all_but_three(s);
        if (lis.stack.size < 3)
                a_sort_three_or_less(s);
        while (s->b.stack.size > 0)