From: Dominik Kaiser <141638109+dpu-kaiser@users.noreply.github.com> Date: Tue, 16 Apr 2024 07:51:49 +0000 (+0200) Subject: Merge branch 'quicksort' into merge-master-into-quicksort X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=a596331e31bf46d5083e1486b240fbcc76bc908e;p=42%2Fpush_swap.git Merge branch 'quicksort' into merge-master-into-quicksort --- a596331e31bf46d5083e1486b240fbcc76bc908e diff --cc sorting.c index 8f5d31f,b5668a9..7006393 --- a/sorting.c +++ b/sorting.c @@@ -10,9 -10,50 +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; + } + + -t_list *stack_sort(t_stack *stack_a, t_stack *stack_b) +void stack_sort(t_psdata *data) { - t_list *cmds = NULL; + int pivot; - - int pivot_idx = stack_a->size - 1; - while (stack_b->size || !is_sorted(stack_a)) ++ int pivot_idx; ++ ++ data->cmds = NULL; ++ pivot_idx = data->a->size - 1; ++ while (data->b->size || !is_sorted(data->a)) + { - pivot = stack_a->stack[pivot_idx]; ++ pivot = data->a->stack[pivot_idx]; - while (stack_a->size > 0 && stack_a->stack[0] != pivot) ++ while (data->a->size > 0 && data->a->stack[0] != pivot) + { - if (stack_a->stack[0] > pivot) - run_command(stack_a, stack_b, &cmds, RA); ++ if (data->a->stack[0] > pivot) ++ run_command(data, RA); + else - run_command(stack_a, stack_b, &cmds, PB); ++ run_command(data, PB); + } - while (stack_b->size > 0) - run_command(stack_a, stack_b, &cmds, PA); - if (stack_a->stack[pivot_idx] == pivot) ++ while (data->b->size > 0) ++ run_command(data, PA); ++ if (data->a->stack[pivot_idx] == pivot) + pivot_idx--; + if (pivot_idx < 0) - pivot_idx = stack_a->size - 1; ++ pivot_idx = sdata->a->size - 1; + } + return cmds; }