diff options
| author | Dominik Kaiser | 2024-04-16 00:01:47 +0200 |
|---|---|---|
| committer | Dominik Kaiser | 2024-04-16 00:01:47 +0200 |
| commit | f421b2dcf9a5d12655e48f4a06f35f17e785fb0f (patch) | |
| tree | fd27f598757f59db733079b9facc693b6c3ec5c8 /sorting.c | |
| parent | 05e786db0ae74da69012e1b5aa2dc9169e56723b (diff) | |
| download | push_swap-f421b2dcf9a5d12655e48f4a06f35f17e785fb0f.tar.gz push_swap-f421b2dcf9a5d12655e48f4a06f35f17e785fb0f.zip | |
Add inefficient quicksort? algorithm
This algorithm is based on quicksort but probably not really in its
spirit. It is extremely inefficient but could maybe fixed with some
post-optimization.
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; } |
