Skip to content

Commit

Permalink
update sort
Browse files Browse the repository at this point in the history
  • Loading branch information
rask24 committed Feb 10, 2024
1 parent dbb83cf commit a42bc12
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 47 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ SRC = $(SRC_DIR)/main.c \
$(SRC_DIR)/sort/sort.c \
$(SRC_DIR)/sort/micro_sort.c \
$(SRC_DIR)/sort/nano_sort.c \
$(SRC_DIR)/sort/large_sort.c \
$(SRC_DIR)/sort/set_cost.c \
$(SRC_DIR)/sort/set_opt.c \
$(SRC_DIR)/sort/greedy_operation.c \
$(SRC_DIR)/sort/is_sorted_stack.c \
$(SRC_DIR)/sort/large_sort/large_sort.c \
$(SRC_DIR)/sort/large_sort/set_cost.c \
$(SRC_DIR)/sort/large_sort/set_opt.c \
$(SRC_DIR)/sort/large_sort/greedy_operation.c \
$(SRC_DIR)/stack_operations/push.c \
$(SRC_DIR)/stack_operations/swap.c \
$(SRC_DIR)/stack_operations/rotate.c \
Expand Down
10 changes: 5 additions & 5 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/02/10 21:12:16 by reasuke ### ########.fr */
/* Updated: 2024/02/10 21:18:02 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -46,10 +46,10 @@ int check_args(int argc, char **argv);
t_stack *generate_stack(int argc, char **argv);
void exit_with_error(void);

void sort(t_stack **p_a, t_stack **p_b, int num_a);
void nano_sort(t_stack **p_a, int num_a);
void micro_sort(t_stack **p_a, t_stack **p_b, int num_a);
void large_sort(t_stack **p_a, t_stack **p_b, int num_a);
void sort(t_stack **p_a, t_stack **p_b);
void nano_sort(t_stack **p_a);
void micro_sort(t_stack **p_a, t_stack **p_b);
void large_sort(t_stack **p_a, t_stack **p_b);
void set_cost(t_stack **p_a, t_stack **p_b);
void set_opt(t_stack **p_b);
void greedy_operation(t_stack **p_a, t_stack **p_b);
Expand Down
4 changes: 2 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/02/07 15:12:35 by reasuke ### ########.fr */
/* Updated: 2024/02/10 21:19:08 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -41,7 +41,7 @@ int main(int argc, char **argv)
check_args(argc, argv);
a = generate_stack(argc, argv);
b = NULL;
sort(&a, &b, argc - 1);
sort(&a, &b);
clear_stack(&a, free);
return (0);
}
File renamed without changes.
29 changes: 12 additions & 17 deletions src/sort/large_sort.c → src/sort/large_sort/large_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,16 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 16:55:10 by reasuke #+# #+# */
/* Updated: 2024/02/10 19:31:10 by reasuke ### ########.fr */
/* Updated: 2024/02/10 21:24:58 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

static void _init_stack(t_stack **p_a, t_stack **p_b, int num_a)
static void _push_b_n_times(t_stack **p_a, t_stack **p_b, int n)
{
int i;

i = 0;
while (i < num_a - 3)
{
while (n--)
operate_pb(p_a, p_b);
i++;
}
}

static void _sort_stack_a(t_stack **p_a, int num_a)
Expand All @@ -38,19 +32,20 @@ static void _sort_stack_a(t_stack **p_a, int num_a)
}
}

void large_sort(t_stack **p_a, t_stack **p_b, int num_a)
void large_sort(t_stack **p_a, t_stack **p_b)
{
int i;
int size_a;
int size_b;

_init_stack(p_a, p_b, num_a);
nano_sort(p_a, 3);
i = 0;
while (i < num_a - 3)
size_a = stack_size(*p_a);
size_b = size_a - 3;
_push_b_n_times(p_a, p_b, size_b);
nano_sort(p_a);
while (size_b--)
{
set_cost(p_a, p_b);
set_opt(p_b);
greedy_operation(p_a, p_b);
i++;
}
_sort_stack_a(p_a, num_a);
_sort_stack_a(p_a, size_a);
}
File renamed without changes.
File renamed without changes.
15 changes: 9 additions & 6 deletions src/sort/micro_sort.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 16:32:27 by reasuke #+# #+# */
/* Updated: 2024/02/10 20:55:11 by reasuke ### ########.fr */
/* Updated: 2024/02/10 21:17:40 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,7 +15,7 @@
static void _handle_4(t_stack **p_a, t_stack **p_b)
{
operate_pb(p_a, p_b);
nano_sort(p_a, 3);
nano_sort(p_a);
if (get_first_index(p_b) == 1)
operate_pa(p_b, p_a);
else if (get_first_index(p_b) == 2)
Expand Down Expand Up @@ -69,7 +69,7 @@ static void _handle_5(t_stack **p_a, t_stack **p_b)
{
operate_pb(p_a, p_b);
operate_pb(p_a, p_b);
nano_sort(p_a, 3);
nano_sort(p_a);
if (get_first_index(p_b) < get_second_index(p_b))
operate_sb(p_b);
if (get_first_index(p_b) == 5 && get_second_index(p_b) == 4)
Expand All @@ -78,10 +78,13 @@ static void _handle_5(t_stack **p_a, t_stack **p_b)
_normal_flow_5(p_a, p_b);
}

void micro_sort(t_stack **p_a, t_stack **p_b, int num_a)
void micro_sort(t_stack **p_a, t_stack **p_b)
{
if (num_a == 4)
int size_a;

size_a = stack_size(*p_a);
if (size_a == 4)
_handle_4(p_a, p_b);
else if (num_a == 5)
else if (size_a == 5)
_handle_5(p_a, p_b);
}
11 changes: 7 additions & 4 deletions src/sort/nano_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 15:58:22 by reasuke #+# #+# */
/* Updated: 2024/02/10 20:56:41 by reasuke ### ########.fr */
/* Updated: 2024/02/10 21:14:43 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -48,10 +48,13 @@ static void _handle_3(t_stack **p_a)
operate_rra(p_a);
}

void nano_sort(t_stack **p_a, int num_a)
void nano_sort(t_stack **p_a)
{
if (num_a == 2)
int size_a;

size_a = stack_size(*p_a);
if (size_a == 2)
_handle_2(p_a);
else if (num_a == 3)
else if (size_a == 3)
_handle_3(p_a);
}
19 changes: 12 additions & 7 deletions src/sort/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@
/* By: reasuke <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 15:10:35 by reasuke #+# #+# */
/* Updated: 2024/02/07 16:35:12 by reasuke ### ########.fr */
/* Updated: 2024/02/10 21:36:16 by reasuke ### ########.fr */
/* */
/* ************************************************************************** */

#include "push_swap.h"

void sort(t_stack **p_a, t_stack **p_b, int num_a)
void sort(t_stack **p_a, t_stack **p_b)
{
if (num_a <= 3)
nano_sort(p_a, num_a);
else if (num_a <= 5)
micro_sort(p_a, p_b, num_a);
int size_a;

if (is_sorted_stack(*p_a))
return ;
size_a = stack_size(*p_a);
if (size_a <= 3)
nano_sort(p_a);
else if (size_a <= 5)
micro_sort(p_a, p_b);
else
large_sort(p_a, p_b, num_a);
large_sort(p_a, p_b);
}
4 changes: 2 additions & 2 deletions test/test_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static void sort_test_main(int N) {
ft_lstadd_back(&stack_a, ft_lstnew(c));
}
// sort
sort(&stack_a, &stack_b, N);
sort(&stack_a, &stack_b);
// check if the order is appropreate
for (int i = 1; stack_a; ++i, stack_a = stack_a->next) {
EXPECT_EQ(get_first_index(&stack_a), i);
Expand Down Expand Up @@ -77,7 +77,7 @@ static void random_sort_test_main(int N) {
ft_lstadd_back(&stack_a, ft_lstnew(c));
}
// sort
sort(&stack_a, &stack_b, N);
sort(&stack_a, &stack_b);
// check if the order is appropreate
for (int i = 1; stack_a; ++i, stack_a = stack_a->next) {
EXPECT_EQ(*(int *)stack_a->content, i);
Expand Down

0 comments on commit a42bc12

Please sign in to comment.