summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2024-04-17 10:51:01 +0200
committerDominik Kaiser2024-04-17 10:51:01 +0200
commit61c619f2d24cadd53defe1cfa74b71f644c49010 (patch)
tree42425b51ebcb023426b7e220ab06f5d0f940a61f
parentca291232e5716bc277b4d56c7a76dac2f8caa860 (diff)
downloadpush_swap-61c619f2d24cadd53defe1cfa74b71f644c49010.tar.gz
push_swap-61c619f2d24cadd53defe1cfa74b71f644c49010.zip
Add presorting
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.
-rw-r--r--sorting.c29
1 files changed, 27 insertions, 2 deletions
diff --git a/sorting.c b/sorting.c
index 8f5d31f..ca21757 100644
--- 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);
}