]> git.dkaiser.de - 42/push_swap.git/commitdiff
Add presorting
authorDominik Kaiser <dominik@dominik-XPS.fritz.box>
Wed, 17 Apr 2024 08:51:01 +0000 (10:51 +0200)
committerDominik Kaiser <dominik@dominik-XPS.fritz.box>
Wed, 17 Apr 2024 08:51:01 +0000 (10:51 +0200)
All entries that are not in order will be pushed into b.
If the entry is larger than our pivot, it will be pushed to the bottom,
else the top of b.

sorting.c

index 8f5d31f35c9ddc7a562215ee00f78587df0f3457..ca21757d7ba18a5347cc2405119ab0066cccec1a 100644 (file)
--- a/sorting.c
+++ b/sorting.c
@@ -6,13 +6,38 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/13 15:04:19 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/04/16 09:27:53 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/04/17 10:50:01 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "push_swap.h"
 
-void stack_sort(t_psdata *data)
+static void presort(t_psdata *data)
 {
+    int size;
+    int pivot;
+    int max;
+
+    size = data->a->size;
+    pivot = size / 2;
+    max = data->a->stack[0];
+    while (size--)
+    {
+        if (data->a->stack[0] < max)
+        {
+            run_command(data, PB);
+            if (data->b->stack[0] > pivot)
+                run_command(data, RB);
+        }
+        else
+        {
+            max = data->a->stack[0];
+            run_command(data, RA);
+        }
+    }
+}
 
+void stack_sort(t_psdata *data)
+{
+    presort(data);
 }