From fa1450d620c56e77dea3b1a82513b190818bf618 Mon Sep 17 00:00:00 2001 From: rask24 <70057885+rask24@users.noreply.github.com> Date: Wed, 17 Jan 2024 18:36:21 +0900 Subject: [PATCH] optimize micro sort (#13) * add check.rb * optimzie micro sort * refactor first / second content * refacotr _handle_5 with 2 flows --- Makefile | 2 ++ include/push_swap.h | 4 ++- src/sort/micro_sort.c | 56 +++++++++++++++++++++----------------- src/utils/first_content.c | 18 ++++++++++++ src/utils/second_content.c | 18 ++++++++++++ test/e2e/check.rb | 14 ++++++++++ 6 files changed, 86 insertions(+), 26 deletions(-) create mode 100644 src/utils/first_content.c create mode 100644 src/utils/second_content.c create mode 100644 test/e2e/check.rb diff --git a/Makefile b/Makefile index 951b024..f69b6a9 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,8 @@ SRC = $(SRC_DIR)/main.c \ $(SRC_DIR)/stack_operations/swap.c \ $(SRC_DIR)/stack_operations/rotate.c \ $(SRC_DIR)/stack_operations/ft_lst_before.c \ + $(SRC_DIR)/utils/first_content.c \ + $(SRC_DIR)/utils/second_content.c \ $(SRC_DIR)/utils/exit_with_error.c OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC)) DEP = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.d, $(SRC)) diff --git a/include/push_swap.h b/include/push_swap.h index 4d88759..9d54ef8 100644 --- a/include/push_swap.h +++ b/include/push_swap.h @@ -6,7 +6,7 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/10 12:37:54 by reasuke #+# #+# */ -/* Updated: 2024/01/17 16:22:03 by reasuke ### ########.fr */ +/* Updated: 2024/01/17 18:29:53 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,6 +43,8 @@ void rotate_stack(t_list **stack); void reverse_rotate_stack(t_list **stack); t_list *ft_lst_before(t_list *lst, t_list *trg); +int first_content(t_list **stack); +int second_content(t_list **stack); void exit_with_error(void); #endif diff --git a/src/sort/micro_sort.c b/src/sort/micro_sort.c index 9ecbab9..5cb8661 100644 --- a/src/sort/micro_sort.c +++ b/src/sort/micro_sort.c @@ -6,72 +6,78 @@ /* By: reasuke +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/15 16:32:27 by reasuke #+# #+# */ -/* Updated: 2024/01/17 17:43:30 by reasuke ### ########.fr */ +/* Updated: 2024/01/17 18:33:18 by reasuke ### ########.fr */ /* */ /* ************************************************************************** */ #include "push_swap.h" -static int _first(t_list **stack) -{ - return (*(int *)(*stack)->content); -} - -static int _second(t_list **stack) -{ - return (*(int *)(*stack)->next->content); -} - static void _handle_4(t_list **stack_a, t_list **stack_b) { operate_pb(stack_a, stack_b); nano_sort(stack_a, 3); - if (_first(stack_b) == 1) + if (first_content(stack_b) == 1) operate_pa(stack_b, stack_a); - else if (_first(stack_b) == 2) + else if (first_content(stack_b) == 2) { operate_pa(stack_b, stack_a); operate_sa(stack_a); } - else if (_first(stack_b) == 3) + else if (first_content(stack_b) == 3) { operate_rra(stack_a); operate_pa(stack_b, stack_a); operate_ra(stack_a); operate_ra(stack_a); } - else if (_first(stack_b) == 4) + else if (first_content(stack_b) == 4) { operate_pa(stack_b, stack_a); operate_ra(stack_a); } } -static void _handle_5(t_list **stack_a, t_list **stack_b) +static void _edge_flow_5(t_list **stack_a, t_list **stack_b) { - operate_pb(stack_a, stack_b); - operate_pb(stack_a, stack_b); - nano_sort(stack_a, 3); - if (_first(stack_b) < _second(stack_b)) - operate_sb(stack_b); - if (_first(stack_b) == 5) + operate_pa(stack_b, stack_a); + operate_pa(stack_b, stack_a); + operate_ra(stack_a); + operate_ra(stack_a); +} + +static void _normal_flow_5(t_list **stack_a, t_list **stack_b) +{ + if (first_content(stack_b) == 5) { operate_pa(stack_b, stack_a); operate_rra(stack_a); } else { - while (_first(stack_a) != _first(stack_b) + 1) + while (first_content(stack_a) != first_content(stack_b) + 1) operate_ra(stack_a); operate_pa(stack_b, stack_a); } - while (_first(stack_a) != _first(stack_b) + 1) + while (first_content(stack_a) != first_content(stack_b) + 1) operate_rra(stack_a); operate_pa(stack_b, stack_a); - while (_first(stack_a) != 1) + while (first_content(stack_a) != 1) operate_rra(stack_a); } +static void _handle_5(t_list **stack_a, t_list **stack_b) +{ + operate_pb(stack_a, stack_b); + operate_pb(stack_a, stack_b); + nano_sort(stack_a, 3); + if (first_content(stack_b) < second_content(stack_b)) + operate_sb(stack_b); + if (first_content(stack_b) == 5 && second_content(stack_b) == 4) + _edge_flow_5(stack_a, stack_b); + else + _normal_flow_5(stack_a, stack_b); +} + void micro_sort(t_list **stack_a, t_list **stack_b, int num_a) { if (num_a == 4) diff --git a/src/utils/first_content.c b/src/utils/first_content.c new file mode 100644 index 0000000..f0d365a --- /dev/null +++ b/src/utils/first_content.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* first_content.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: reasuke +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/17 18:27:37 by reasuke #+# #+# */ +/* Updated: 2024/01/17 18:29:06 by reasuke ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int first_content(t_list **stack) +{ + return (*(int *)(*stack)->content); +} diff --git a/src/utils/second_content.c b/src/utils/second_content.c new file mode 100644 index 0000000..4721064 --- /dev/null +++ b/src/utils/second_content.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* second_content.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: reasuke +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/17 18:28:02 by reasuke #+# #+# */ +/* Updated: 2024/01/17 18:29:17 by reasuke ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int second_content(t_list **stack) +{ + return (*(int *)(*stack)->next->content); +} diff --git a/test/e2e/check.rb b/test/e2e/check.rb new file mode 100644 index 0000000..fdd6a20 --- /dev/null +++ b/test/e2e/check.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +a = [1, 2, 3, 4, 5] + +a.permutation.to_a.each do |array| + str = +'./push_swap' + array.each do |item| + str << " #{item}" + end + str << ' | wc -l | bc' + puts str + system str, exception: true + puts "\n" +end