Skip to content

Commit

Permalink
implement micro sort case 4
Browse files Browse the repository at this point in the history
  • Loading branch information
rask24 committed Jan 15, 2024
1 parent 8caa867 commit 225a6a6
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/check_args.c \
$(SRC_DIR)/generate_stack.c \
$(SRC_DIR)/sort/sort.c \
$(SRC_DIR)/sort/micro_sort.c \
$(SRC_DIR)/sort/nano_sort.c \
$(SRC_DIR)/stack_operations/operation.c \
$(SRC_DIR)/stack_operations/push.c \
Expand Down
5 changes: 3 additions & 2 deletions 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/15 16:04:32 by reasuke ### ########.fr */
/* Updated: 2024/01/15 16:31:59 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -27,8 +27,9 @@ int check_args(int argc, char **argv);

t_list *generate_stack(int argc, char **argv);

void sort(t_list **stack, int argc);
void sort(t_list **stack_a, t_list **stack_b, int argc);
void nano_sort(t_list **stack, int num_of_element);
void micro_sort(t_list **stack_a, t_list **stack_b);

void operate_sa(t_list **stack_a);
void operate_ra(t_list **stack_a);
Expand Down
6 changes: 4 additions & 2 deletions src/main.c
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:36 by reasuke #+# #+# */
/* Updated: 2024/01/15 15:24:24 by reasuke ### ########.fr */
/* Updated: 2024/01/15 16:34:16 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,11 +34,13 @@ void put_void(void *content)
int main(int argc, char **argv)
{
t_list *stack_a;
t_list *stack_b;

check_args(argc, argv);
stack_a = generate_stack(argc, argv);
stack_b = NULL;
ft_lstiter(stack_a, put_void);
sort(&stack_a, argc);
sort(&stack_a, &stack_b, argc);
ft_lstiter(stack_a, put_void);
ft_lstclear(&stack_a, free);
return (0);
Expand Down
48 changes: 48 additions & 0 deletions src/sort/micro_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* micro_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 16:32:27 by reasuke #+# #+# */
/* Updated: 2024/01/15 16:52:45 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

void micro_sort(t_list **stack_a, t_list **stack_b)
{
int tmp;

push_stack(stack_a, stack_b);
ft_putendl_fd("pb", STDOUT_FILENO);
tmp = *(int *)(*stack_b)->content;
nano_sort(stack_a, 3);
if (tmp == 1)
{
push_stack(stack_b, stack_a);
ft_putendl_fd("pa", STDOUT_FILENO);
}
if (tmp == 2)
{
push_stack(stack_b, stack_a);
ft_putendl_fd("pa", STDOUT_FILENO);
operate_sa(stack_a);
}
else if (tmp == 3)
{
operate_rra(stack_a);
push_stack(stack_b, stack_a);
ft_putendl_fd("pa", STDOUT_FILENO);
operate_ra(stack_a);
operate_ra(stack_a);
}
else if (tmp == 4)
{
push_stack(stack_b, stack_a);
ft_putendl_fd("pa", STDOUT_FILENO);
operate_ra(stack_a);
}
}
2 changes: 1 addition & 1 deletion src/sort/nano_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static void _handle_3(t_list **stack)
operate_sa(stack);
operate_ra(stack);
}
else if (first == 2 && second == 3 && third == 1)
else if (third < first && first < second)
operate_rra(stack);
}

Expand Down
8 changes: 5 additions & 3 deletions src/sort/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 15:10:35 by reasuke #+# #+# */
/* Updated: 2024/01/15 15:12:31 by reasuke ### ########.fr */
/* Updated: 2024/01/15 16:31:27 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

void sort(t_list **stack, int argc)
void sort(t_list **stack_a, t_list **stack_b, int argc)
{
int num_of_element;

num_of_element = argc - 1;
if (num_of_element <= 3)
nano_sort(stack, num_of_element);
nano_sort(stack_a, num_of_element);
else if (num_of_element == 4)
micro_sort(stack_a, stack_b);
}
4 changes: 2 additions & 2 deletions src/stack_operations/operation.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 15:32:19 by reasuke #+# #+# */
/* Updated: 2024/01/15 15:33:01 by reasuke ### ########.fr */
/* Updated: 2024/01/15 16:48:46 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -26,6 +26,6 @@ void operate_ra(t_list **stack_a)

void operate_rra(t_list **stack_a)
{
swap_stack(stack_a);
reverse_rotate_stack(stack_a);
ft_putendl_fd("rra", STDOUT_FILENO);
}

0 comments on commit 225a6a6

Please sign in to comment.