diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2021-09-11 13:42:30 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2021-09-11 13:42:30 +0200 |
| commit | f90e5ce2481631f2d0bd2bf16d33312ba835e93a (patch) | |
| tree | 3c40af6cdc5d458cdfa96045e37d2c520a7f1fa9 | |
| parent | 48fc8d0ffa3bfb766d28bce858eccb68fb0fdec5 (diff) | |
| download | push_swap-f90e5ce2481631f2d0bd2bf16d33312ba835e93a.tar.gz push_swap-f90e5ce2481631f2d0bd2bf16d33312ba835e93a.tar.bz2 push_swap-f90e5ce2481631f2d0bd2bf16d33312ba835e93a.zip | |
| -rw-r--r-- | src/push_swap/sort.c | 82 |
1 files changed, 58 insertions, 24 deletions
diff --git a/src/push_swap/sort.c b/src/push_swap/sort.c index b2d4838..9b1280e 100644 --- a/src/push_swap/sort.c +++ b/src/push_swap/sort.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/19 09:12:02 by cacharle #+# #+# */ -/* Updated: 2021/09/10 19:35:34 by charles ### ########.fr */ +/* Updated: 2021/09/11 13:41:33 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,35 +17,63 @@ static int frame_length(t_stack *st, int frame_index) return (stack_length(st) - frame_index); } -static void push_swap_sort3(t_stack *main) +static int stack_cmp(t_stack *stack, int x, int y) { - int a, b, c; + if (stack->tag == STACK_A) + return (x < y); + else + return (x > y); +} - a = main->elements[main->top]; - b = main->elements[main->top - 1]; - c = main->elements[main->top - 2]; +/* +** stack is in reverse order compared to expected_orders +*/ - /* 2 3 1 rra */ - if (a < b && b > c && a > c) +int has_order(t_stack *stack, int *expected_orders, int expected_orders_len) +{ + int i; + int j; + int count = 0; + + i = -1; + while (++i < expected_orders_len) + { + count = 0; + j = -1; + while (++j < stack_length(stack)) + { + if (stack_cmp(stack, stack->elements[j], stack->elements[expected_orders_len - 1 - i])) + count++; + } + if (count != expected_orders[i]) + return (false); + } + return (true); +} + +static void push_swap_sort3(t_stack *main) +{ + /* 1 2 0 rra */ + if (has_order(main, (int[]){1, 2, 0}, 3)) stack_reverse_rotate_print(main); - /* 3 1 2 ra */ - else if (a > b && b < c && a > c) + /* 2 0 1 ra */ + else if (has_order(main, (int[]){2, 0, 1}, 3)) stack_rotate_print(main); - /* 3 2 1 ra sa */ - else if (a > b && b > c && a > c) + /* 2 1 0 ra sa */ + else if (has_order(main, (int[]){2, 1, 0}, 3)) { stack_rotate_print(main); stack_swap_print(main); } - /* 2 1 3 sa */ - else if (a > b && b < c && a < c) + /* 1 0 2 sa */ + else if (has_order(main, (int[]){1, 0, 2}, 3)) stack_swap_print(main); - /* 1 3 2 sa ra*/ - else if (a < b && b > c && a < c) + /* 0 2 1 sa ra */ + else if (has_order(main, (int[]){0, 2, 1}, 3)) { stack_swap_print(main); stack_rotate_print(main); @@ -53,13 +81,6 @@ static void push_swap_sort3(t_stack *main) } -static int stack_cmp(t_stack *stack, int x, int y) -{ - if (stack->tag == STACK_A) - return (x < y); - else - return (x > y); -} /* ** main : stack to sort @@ -146,7 +167,20 @@ static void push_swap_qsort_rec(t_stack *main, t_stack *tmp, return ; if (stack_frame_sorted(main, frame_length(main, main_frame))) return ; - if (main->tag == STACK_A && stack_length(main) == 3) + if (stack_length(main) == 4) + { + stack_push_to_print(main, tmp); + push_swap_sort3(main); + stack_push_to_print(tmp, main); + + } + + /* 3 2 4 1 sa */ + /* 2 3 4 1 sort3 */ + /* 2 1 3 4 sa */ + /* 1 2 3 4 */ + + if (stack_length(main) == 3) return push_swap_sort3(main); if (frame_length(main, main_frame) == 2) { |
