diff options
| author | Dominik Kaiser | 2024-04-16 00:15:03 +0200 |
|---|---|---|
| committer | Dominik Kaiser | 2024-04-16 00:15:03 +0200 |
| commit | 05ed24cc61175dd74056bd0b2ecd509b995784f8 (patch) | |
| tree | fd27f598757f59db733079b9facc693b6c3ec5c8 /sorting.c | |
| parent | 4f288f94c8ec8ced5200d2e4e5713d9b119aed48 (diff) | |
| download | push_swap-05ed24cc61175dd74056bd0b2ecd509b995784f8.tar.gz push_swap-05ed24cc61175dd74056bd0b2ecd509b995784f8.zip | |
Try again to add this algorithm
Hopefully this time I put it in the correct branch.
Diffstat (limited to 'sorting.c')
| -rw-r--r-- | sorting.c | 44 |
1 files changed, 42 insertions, 2 deletions
@@ -6,14 +6,54 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/13 15:04:19 by dkaiser #+# #+# */ -/* Updated: 2024/04/15 17:54:23 by dkaiser ### ########.fr */ +/* Updated: 2024/04/15 23:39:57 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ +#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; +} + t_list *stack_sort(t_stack *stack_a, t_stack *stack_b) { - return NULL; + t_list *cmds = NULL; + int pivot; + + int pivot_idx = stack_a->size - 1; + while (stack_b->size || !is_sorted(stack_a)) + { + pivot = stack_a->stack[pivot_idx]; + + while (stack_a->size > 0 && stack_a->stack[0] != pivot) + { + if (stack_a->stack[0] > pivot) + run_command(stack_a, stack_b, &cmds, RA); + else + run_command(stack_a, stack_b, &cmds, PB); + } + while (stack_b->size > 0) + run_command(stack_a, stack_b, &cmds, PA); + if (stack_a->stack[pivot_idx] == pivot) + pivot_idx--; + if (pivot_idx < 0) + pivot_idx = stack_a->size - 1; + } + return cmds; } |
