summaryrefslogtreecommitdiff
path: root/sorting.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-04-16 09:51:49 +0200
committerGitHub2024-04-16 09:51:49 +0200
commita596331e31bf46d5083e1486b240fbcc76bc908e (patch)
treeb2e20f9eecdd4199a33d33af7c69a7a0a4a82bff /sorting.c
parent571c2f537954fd0d5deb616380276d1d23049aa9 (diff)
parent05ed24cc61175dd74056bd0b2ecd509b995784f8 (diff)
downloadpush_swap-a596331e31bf46d5083e1486b240fbcc76bc908e.tar.gz
push_swap-a596331e31bf46d5083e1486b240fbcc76bc908e.zip
Merge branch 'quicksort' into merge-master-into-quicksort
Diffstat (limited to 'sorting.c')
-rw-r--r--sorting.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/sorting.c b/sorting.c
index 8f5d31f..7006393 100644
--- a/sorting.c
+++ b/sorting.c
@@ -10,9 +10,51 @@
/* */
/* ************************************************************************** */
+#include "libft/ft_printf.h"
+#include "libft/libft.h"
#include "push_swap.h"
+int is_sorted(t_stack *stack)
+{
+ int i;
+
+ if (stack->size < 2)
+ return 1;
+ i = 1;
+ while (i < stack->size)
+ {
+ if (stack->stack[i] < stack->stack[i-1])
+ return 0;
+ i++;
+ }
+ return 1;
+}
+
+
void stack_sort(t_psdata *data)
{
+ int pivot;
+ int pivot_idx;
+
+ data->cmds = NULL;
+ pivot_idx = data->a->size - 1;
+ while (data->b->size || !is_sorted(data->a))
+ {
+ pivot = data->a->stack[pivot_idx];
+ while (data->a->size > 0 && data->a->stack[0] != pivot)
+ {
+ if (data->a->stack[0] > pivot)
+ run_command(data, RA);
+ else
+ run_command(data, PB);
+ }
+ while (data->b->size > 0)
+ run_command(data, PA);
+ if (data->a->stack[pivot_idx] == pivot)
+ pivot_idx--;
+ if (pivot_idx < 0)
+ pivot_idx = sdata->a->size - 1;
+ }
+ return cmds;
}