Skip to content

Commit

Permalink
optimize micro sort (#13)
Browse files Browse the repository at this point in the history
* add check.rb

* optimzie micro sort

* refactor first / second content

* refacotr _handle_5 with 2 flows
  • Loading branch information
rask24 committed Jan 17, 2024
1 parent 12638ed commit fa1450d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 26 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion include/push_swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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
56 changes: 31 additions & 25 deletions src/sort/micro_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,72 +6,78 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
Expand Down
18 changes: 18 additions & 0 deletions src/utils/first_content.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* first_content.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
18 changes: 18 additions & 0 deletions src/utils/second_content.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* second_content.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
14 changes: 14 additions & 0 deletions test/e2e/check.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fa1450d

Please sign in to comment.