From 5f6bdaec71242667fc0cb67888405c742fdf51f5 Mon Sep 17 00:00:00 2001 From: Willem Haffmans Date: Sun, 24 Nov 2024 15:19:43 +0000 Subject: [PATCH] implemented LIS optimalisation and split all the function into their own file --- .gitignore | 1 + Makefile | 11 ++- inc/push_swap.h | 15 +++- src/checker.c | 34 ++++++++ src/moves/push.c | 19 ----- src/moves/push_all_but_3_b.c | 24 ++++++ src/moves/push_all_but_lis.c | 37 +++++++++ src/moves/put_at_index_asc.c | 23 ++++++ src/moves/rotate_a.c | 27 +++++++ src/moves/{rotate_utils.c => rotate_a_to.c} | 49 +---------- src/moves/rotate_a_to_top.c | 19 +++++ src/moves/rotate_b.c | 27 +++++++ src/moves/shortest_rotate.c | 18 +++++ src/push_swap.c | 54 ++----------- src/sort/b_merge_a.c | 28 +++++++ src/sort/best_merge_a.c | 38 +++++++++ src/sort/common_rotations.c | 38 +++++++++ src/sort/count_moves_merge_a.c | 25 ++++++ src/sort/sortmore.c | 90 +++------------------ src/util/count_gt.c | 27 +++++++ src/util/error.c | 21 +++++ src/util/free_state.c | 26 ++++++ src/util/ft_abs.c | 18 +++++ src/util/ft_max.c | 18 +++++ src/util/has_duplicates.c | 31 +++++++ src/util/{utils.c => index_of.c} | 35 +------- src/util/initialise_state.c | 25 ++++++ src/util/is_sorted.c | 24 ++++++ src/util/lis_indices.c | 36 +++++++++ src/util/lis_lengths.c | 43 ++++++++++ src/util/longest_incremental_subsequence.c | 43 ++++++++++ src/util/print_stack.c | 35 ++++++++ src/util/print_state.c | 21 +++++ tester.sh | 46 +++++++---- 34 files changed, 784 insertions(+), 242 deletions(-) create mode 100644 src/checker.c create mode 100644 src/moves/push_all_but_3_b.c create mode 100644 src/moves/push_all_but_lis.c create mode 100644 src/moves/put_at_index_asc.c create mode 100644 src/moves/rotate_a.c rename src/moves/{rotate_utils.c => rotate_a_to.c} (54%) create mode 100644 src/moves/rotate_a_to_top.c create mode 100644 src/moves/rotate_b.c create mode 100644 src/moves/shortest_rotate.c create mode 100644 src/sort/b_merge_a.c create mode 100644 src/sort/best_merge_a.c create mode 100644 src/sort/common_rotations.c create mode 100644 src/sort/count_moves_merge_a.c create mode 100644 src/util/count_gt.c create mode 100644 src/util/error.c create mode 100644 src/util/free_state.c create mode 100644 src/util/ft_abs.c create mode 100644 src/util/ft_max.c create mode 100644 src/util/has_duplicates.c rename src/util/{utils.c => index_of.c} (63%) create mode 100644 src/util/initialise_state.c create mode 100644 src/util/is_sorted.c create mode 100644 src/util/lis_indices.c create mode 100644 src/util/lis_lengths.c create mode 100644 src/util/longest_incremental_subsequence.c create mode 100644 src/util/print_stack.c create mode 100644 src/util/print_state.c mode change 100644 => 100755 tester.sh diff --git a/.gitignore b/.gitignore index 36b8e97..8bb8cf9 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ obj/ Session.vim test.txt checker_linux +.vscode/ diff --git a/Makefile b/Makefile index b0d4e45..5bf9c92 100644 --- a/Makefile +++ b/Makefile @@ -20,8 +20,15 @@ OBJ_PATH = obj VPATH = src:src/util:src/moves:src/sort -SOURCES = push_swap.c ft_lstat.c new_element.c push.c rotate.c reverse_rotate.c \ - swap.c sort3.c stack_min.c utils.c sortmore.c rotate_utils.c +SOURCES = push_all_but_3_b.c push_all_but_lis.c push.c put_at_index_asc.c \ + reverse_rotate.c rotate_a.c rotate_a_to.c rotate_a_to_top.c \ + rotate_b.c rotate.c shortest_rotate.c swap.c push_swap.c \ + best_merge_a.c b_merge_a.c common_rotations.c count_moves_merge_a.c \ + sort3.c sortmore.c count_gt.c error.c free_state.c ft_abs.c \ + ft_lstat.c ft_max.c has_duplicates.c index_of.c initialise_state.c \ + is_sorted.c lis_indices.c lis_lengths.c \ + longest_incremental_subsequence.c new_element.c print_stack.c \ + print_state.c stack_min.c OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) diff --git a/inc/push_swap.h b/inc/push_swap.h index ba4865a..809b692 100644 --- a/inc/push_swap.h +++ b/inc/push_swap.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/11/04 13:58:35 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/11/10 16:24:06 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/11/24 15:18:08 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ @@ -20,6 +20,19 @@ typedef struct s_state t_list *a; t_list *b; } t_state; + +t_state *initialise_state(t_state **state); +int *lis_lengths(t_list *stack, int size); +int *lis_indices(int *lengths, int size, int max_len); +void b_merge_a(int idx_b, t_state *state); +int best_merge_a(t_state *state); +int count_moves_merge_a(int idx_b, t_state *state); +int error(t_state *state); +void free_state(t_state *state); +int is_sorted(t_list *stack); +int has_duplicates(t_list *stack); +int *longest_incremental_subsequence(t_list *stack, int *lis_size); +void push_all_but_lis(t_state *state); t_list *ft_lstat(t_list *list, int n); int ft_abs(int a); int ft_max(int a, int b); diff --git a/src/checker.c b/src/checker.c new file mode 100644 index 0000000..b54d826 --- /dev/null +++ b/src/checker.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* checker.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:05:06 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:05:43 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int main(int argc, char *argv[]) +{ + t_state *state; + + state = NULL; + if (!initialise_state(&state)) + return (1); + while (argc-- > 1) + { + if (!new_element(&(state->a), argv[argc])) + return (error(state)); + if (has_duplicates(state->a)) + return (error(state)); + } + if (ft_lstsize(state->a) <= 1 || is_sorted(state->a)) + return (0); + sortmore(state); + free_state(state); + return (0); +} diff --git a/src/moves/push.c b/src/moves/push.c index 407c6f1..54f40eb 100644 --- a/src/moves/push.c +++ b/src/moves/push.c @@ -13,25 +13,6 @@ #include "libft.h" #include "push_swap.h" -int put_at_index_asc(int number, t_list *stack) -{ - const int i = index_of(stack_min(stack), stack); - const int size = ft_lstsize(stack); - const int gt = count_gt(number, stack); - - return ((i + gt) % size); -} - -void push_all_but_3_b(t_state *state) -{ - const int size = ft_lstsize(state->a); - int i; - - i = 0; - while (i++ < size - 3) - pb(state); -} - void pb(t_state *state) { push(&(state->a), &(state->b)); diff --git a/src/moves/push_all_but_3_b.c b/src/moves/push_all_but_3_b.c new file mode 100644 index 0000000..0f9d6db --- /dev/null +++ b/src/moves/push_all_but_3_b.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* push_all_but_3_b.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:15:47 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:15:49 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" +#include "libft.h" + +void push_all_but_3_b(t_state *state) +{ + const int size = ft_lstsize(state->a); + int i; + + i = 0; + while (i++ < size - 3) + pb(state); +} diff --git a/src/moves/push_all_but_lis.c b/src/moves/push_all_but_lis.c new file mode 100644 index 0000000..44c0765 --- /dev/null +++ b/src/moves/push_all_but_lis.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* push_all_but_lis.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:15:53 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:16:11 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void push_all_but_lis(t_state *state) +{ + const int size = ft_lstsize(state->a); + int lis_size; + int i; + int j; + int *lis_indices; + + i = 0; + j = 0; + lis_indices = longest_incremental_subsequence(state->a, &lis_size); + while (i < size) + { + if (j < lis_size && i == lis_indices[j]) + { + j++; + ra(state); + } + else + pb(state); + i++; + } +} diff --git a/src/moves/put_at_index_asc.c b/src/moves/put_at_index_asc.c new file mode 100644 index 0000000..f53d7bd --- /dev/null +++ b/src/moves/put_at_index_asc.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* put_at_index_asc.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:16:18 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:16:19 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" +#include "libft.h" + +int put_at_index_asc(int number, t_list *stack) +{ + const int i = index_of(stack_min(stack), stack); + const int size = ft_lstsize(stack); + const int gt = count_gt(number, stack); + + return ((i + gt) % size); +} diff --git a/src/moves/rotate_a.c b/src/moves/rotate_a.c new file mode 100644 index 0000000..7260823 --- /dev/null +++ b/src/moves/rotate_a.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* rotate_a.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:16:27 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:16:28 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void rotate_a(int n, t_state *state) +{ + while (n > 0) + { + ra(state); + n--; + } + while (n < 0) + { + rra(state); + n++; + } +} diff --git a/src/moves/rotate_utils.c b/src/moves/rotate_a_to.c similarity index 54% rename from src/moves/rotate_utils.c rename to src/moves/rotate_a_to.c index 9177b9a..c7f604f 100644 --- a/src/moves/rotate_utils.c +++ b/src/moves/rotate_a_to.c @@ -1,60 +1,17 @@ /* ************************************************************************** */ /* */ /* ::: o_ :::::: ::: */ -/* rotate_utils.c :+: / :+::+: :+: */ +/* rotate_a_to.c :+: / :+::+: :+: */ /* +:+ > +:++:+ +:+ */ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2024/11/19 15:03:32 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/11/19 15:03:35 by whaffman ### ### ### ### / \ */ +/* Created: 2024/11/24 15:16:34 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:16:37 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "push_swap.h" -int rotate_a_to_top(t_state *state) -{ - rotate_a_to(index_of(stack_min(state->a), state->a), state); - return (1); -} - -int shortest_rotate(int index, int size) -{ - if (index <= size / 2) - return (index); - return (-1 * (size - index)); -} - -//TODO -void rotate_a(int n, t_state *state) -{ - while (n > 0) - { - ra(state); - n--; - } - while (n < 0) - { - rra(state); - n++; - } -} - -void rotate_b(int n, t_state *state) -{ - while (n > 0) - { - rb(state); - n--; - } - while (n < 0) - { - rrb(state); - n++; - } -} - void rotate_a_to(int index, t_state *state) { int n; diff --git a/src/moves/rotate_a_to_top.c b/src/moves/rotate_a_to_top.c new file mode 100644 index 0000000..391cd89 --- /dev/null +++ b/src/moves/rotate_a_to_top.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* rotate_a_to_top.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:16:43 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:16:44 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int rotate_a_to_top(t_state *state) +{ + rotate_a_to(index_of(stack_min(state->a), state->a), state); + return (1); +} diff --git a/src/moves/rotate_b.c b/src/moves/rotate_b.c new file mode 100644 index 0000000..062aa36 --- /dev/null +++ b/src/moves/rotate_b.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* rotate_b.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:16:49 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:16:51 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void rotate_b(int n, t_state *state) +{ + while (n > 0) + { + rb(state); + n--; + } + while (n < 0) + { + rrb(state); + n++; + } +} diff --git a/src/moves/shortest_rotate.c b/src/moves/shortest_rotate.c new file mode 100644 index 0000000..6efdca2 --- /dev/null +++ b/src/moves/shortest_rotate.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* shortest_rotate.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:16:56 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:16:57 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +int shortest_rotate(int index, int size) +{ + if (index <= size / 2) + return (index); + return (-1 * (size - index)); +} diff --git a/src/push_swap.c b/src/push_swap.c index ea4127f..daf32b2 100644 --- a/src/push_swap.c +++ b/src/push_swap.c @@ -6,54 +6,12 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/11/04 13:04:11 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/11/07 15:26:58 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/11/24 15:06:16 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ #include "libft.h" #include "push_swap.h" -#include -#include - -void print_stack(const char *name, t_list *stack) -{ - int lst_size; - t_list *node; - - ft_printf("%s |", name); - if (!stack) - { - ft_printf("\n"); - return ; - } - lst_size = ft_lstsize(stack); - while (lst_size > 0) - { - node = ft_lstat(stack, lst_size - 1); - ft_printf("%d ", *((int *) node->content)); - lst_size--; - } - ft_printf("\n"); -} - -void print_state(t_state *state) -{ - ft_printf("\n"); - print_stack("A", state->a); - print_stack("B", state->b); - ft_printf("===================\n"); -} - -t_state *initialise_state(t_state **state) -{ - *state = malloc(sizeof (t_state)); - if (*state) - { - (*state)->a = NULL; - (*state)->b = NULL; - } - return (*state); -} int main(int argc, char *argv[]) { @@ -65,11 +23,13 @@ int main(int argc, char *argv[]) while (argc-- > 1) { if (!new_element(&(state->a), argv[argc])) - { - ft_putstr_fd("Error\n", 2); - return (1); - } + return (error(state)); + if (has_duplicates(state->a)) + return (error(state)); } + if (ft_lstsize(state->a) <= 1 || is_sorted(state->a)) + return (0); sortmore(state); + free_state(state); return (0); } diff --git a/src/sort/b_merge_a.c b/src/sort/b_merge_a.c new file mode 100644 index 0000000..0c6266a --- /dev/null +++ b/src/sort/b_merge_a.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* b_merge_a.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:15:10 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:15:12 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void b_merge_a(int idx_b, t_state *state) +{ + const int number_b = *(int *) ft_lstat(state->b, idx_b)->content; + const int idx_a = put_at_index_asc(number_b, state->a); + int shortest_a; + int shortest_b; + + shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a)); + shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b)); + common_rotations(&shortest_a, &shortest_b, state); + rotate_a(shortest_a, state); + rotate_b(shortest_b, state); + pa(state); +} diff --git a/src/sort/best_merge_a.c b/src/sort/best_merge_a.c new file mode 100644 index 0000000..f25d742 --- /dev/null +++ b/src/sort/best_merge_a.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* best_merge_a.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:15:18 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:15:20 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" +#include + +int best_merge_a(t_state *state) +{ + const int size = ft_lstsize(state->b); + int moves; + int best_b; + int min_moves; + int i; + + min_moves = INT_MAX; + best_b = 0; + i = 0; + while (i < size) + { + moves = count_moves_merge_a(i, state); + if (moves < min_moves) + { + min_moves = moves; + best_b = i; + } + i++; + } + return (best_b); +} diff --git a/src/sort/common_rotations.c b/src/sort/common_rotations.c new file mode 100644 index 0000000..dd4298d --- /dev/null +++ b/src/sort/common_rotations.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* common_rotations.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:15:24 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:15:26 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void common_rotations(int *shortest_a, int *shortest_b, t_state *state) +{ + if (*shortest_a * *shortest_b > 0) + { + if (*shortest_a > 0) + { + while (*shortest_a > 0 && *shortest_b > 0) + { + rr(state); + (*shortest_a)--; + (*shortest_b)--; + } + } + else + { + while (*shortest_a < 0 && *shortest_b < 0) + { + rrr(state); + (*shortest_a)++; + (*shortest_b)++; + } + } + } +} diff --git a/src/sort/count_moves_merge_a.c b/src/sort/count_moves_merge_a.c new file mode 100644 index 0000000..7523806 --- /dev/null +++ b/src/sort/count_moves_merge_a.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* count_moves_merge_a.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:15:31 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:15:32 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int count_moves_merge_a(int idx_b, t_state *state) +{ + const int number_b = *(int *) ft_lstat(state->b, idx_b)->content; + const int idx_a = put_at_index_asc(number_b, state->a); + const int shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a)); + const int shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b)); + + if (shortest_a * shortest_b > 0) + return (ft_max(ft_abs(shortest_a), ft_abs(shortest_b)) + 1); + return (ft_abs(shortest_a) + ft_abs(shortest_b) + 1); +} diff --git a/src/sort/sortmore.c b/src/sort/sortmore.c index 5e4580d..fcabd81 100644 --- a/src/sort/sortmore.c +++ b/src/sort/sortmore.c @@ -6,94 +6,22 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/11/17 17:25:40 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/11/17 17:39:12 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/11/24 15:15:39 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ #include "push_swap.h" -#include - -int count_moves_merge_a(int idx_b, t_state *state) -{ - const int number_b = *(int *) ft_lstat(state->b, idx_b)->content; - const int idx_a = put_at_index_asc(number_b, state->a); - const int shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a)); - const int shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b)); - - if (shortest_a * shortest_b > 0) - return (ft_max(ft_abs(shortest_a), ft_abs(shortest_b)) + 1); - return (ft_abs(shortest_a) + ft_abs(shortest_b) + 1); -} - -int best_merge_a(t_state *state) -{ - const int size = ft_lstsize(state->b); - int moves; - int best_b; - int min_moves; - int i; - - min_moves = INT_MAX; - best_b = 0; - i = 0; - while (i < size) - { - moves = count_moves_merge_a(i, state); - if (moves < min_moves) - { - min_moves = moves; - best_b = i; - } - i++; - } - return (best_b); -} - -void b_merge_a(int idx_b, t_state *state) -{ - const int number_b = *(int *) ft_lstat(state->b, idx_b)->content; - const int idx_a = put_at_index_asc(number_b, state->a); - int shortest_a; - int shortest_b; - - shortest_a = shortest_rotate(idx_a, ft_lstsize(state->a)); - shortest_b = shortest_rotate(idx_b, ft_lstsize(state->b)); - common_rotations(&shortest_a, &shortest_b, state); - rotate_a(shortest_a, state); - rotate_b(shortest_b, state); - pa(state); -} - -void common_rotations(int *shortest_a, int *shortest_b, t_state *state) -{ - if (*shortest_a * *shortest_b > 0) - { - if (*shortest_a > 0) - { - while (*shortest_a > 0 && *shortest_b > 0) - { - rr(state); - (*shortest_a)--; - (*shortest_b)--; - } - } - else - { - while (*shortest_a < 0 && *shortest_b < 0) - { - rrr(state); - (*shortest_a)++; - (*shortest_b)++; - } - } - } -} void sortmore(t_state *state) { - push_all_but_3_b(state); - if (ft_lstsize(state->a) == 3) - sort3(state); + if (ft_lstsize(state->a) <= 5) + { + push_all_but_3_b(state); + if (ft_lstsize(state->a) == 3) + sort3(state); + } + else + push_all_but_lis(state); while (state->b) b_merge_a(best_merge_a(state), state); rotate_a_to_top(state); diff --git a/src/util/count_gt.c b/src/util/count_gt.c new file mode 100644 index 0000000..8357d0e --- /dev/null +++ b/src/util/count_gt.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* count_gt.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:15:02 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:15:04 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int count_gt(int n, t_list *stack) +{ + int count; + + count = 0; + while (stack) + { + if (*(int *) stack->content < n) + count++; + stack = stack->next; + } + return (count); +} diff --git a/src/util/error.c b/src/util/error.c new file mode 100644 index 0000000..5d74eac --- /dev/null +++ b/src/util/error.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* error.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:14:55 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:14:58 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "push_swap.h" + +int error(t_state *state) +{ + free_state(state); + ft_putstr_fd("Error\n", 2); + return (1); +} diff --git a/src/util/free_state.c b/src/util/free_state.c new file mode 100644 index 0000000..914324e --- /dev/null +++ b/src/util/free_state.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* free_state.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:14:32 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:14:51 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "push_swap.h" +#include + +void free_state(t_state *state) +{ + if (!state) + return ; + if (state->a) + ft_lstclear(&(state->a), free); + if (state->b) + ft_lstclear(&(state->b), free); + free(state); +} diff --git a/src/util/ft_abs.c b/src/util/ft_abs.c new file mode 100644 index 0000000..b922a55 --- /dev/null +++ b/src/util/ft_abs.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_abs.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:14:26 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:14:27 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +int ft_abs(int a) +{ + if (a >= 0) + return (a); + return (-a); +} diff --git a/src/util/ft_max.c b/src/util/ft_max.c new file mode 100644 index 0000000..1fd0440 --- /dev/null +++ b/src/util/ft_max.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* ft_max.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:14:17 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:14:20 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +int ft_max(int a, int b) +{ + if (a > b) + return (a); + return (b); +} diff --git a/src/util/has_duplicates.c b/src/util/has_duplicates.c new file mode 100644 index 0000000..aa9bf05 --- /dev/null +++ b/src/util/has_duplicates.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* has_duplicates.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:14:06 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:14:13 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int has_duplicates(t_list *stack) +{ + t_list *node; + + while (stack) + { + node = stack->next; + while (node) + { + if (*(int *) stack->content == *(int *) node->content) + return (1); + node = node->next; + } + stack = stack->next; + } + return (0); +} diff --git a/src/util/utils.c b/src/util/index_of.c similarity index 63% rename from src/util/utils.c rename to src/util/index_of.c index 67e16e8..767ac61 100644 --- a/src/util/utils.c +++ b/src/util/index_of.c @@ -1,16 +1,15 @@ /* ************************************************************************** */ /* */ /* ::: o_ :::::: ::: */ -/* utils.c :+: / :+::+: :+: */ +/* index_of.c :+: / :+::+: :+: */ /* +:+ > +:++:+ +:+ */ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2024/11/10 14:44:51 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/11/17 17:41:19 by whaffman ### ### ### ### / \ */ +/* Created: 2024/11/24 15:14:00 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:14:01 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "push_swap.h" int index_of(int number, t_list *stack) @@ -27,31 +26,3 @@ int index_of(int number, t_list *stack) } return (-1); } - -int ft_abs(int a) -{ - if (a >= 0) - return (a); - return (-a); -} - -int ft_max(int a, int b) -{ - if (a > b) - return (a); - return (b); -} - -int count_gt(int n, t_list *stack) -{ - int count; - - count = 0; - while (stack) - { - if (*(int *) stack->content < n) - count++; - stack = stack->next; - } - return (count); -} diff --git a/src/util/initialise_state.c b/src/util/initialise_state.c new file mode 100644 index 0000000..c82cf7f --- /dev/null +++ b/src/util/initialise_state.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* initialise_state.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:13:53 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:13:55 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" +#include + +t_state *initialise_state(t_state **state) +{ + *state = malloc(sizeof (t_state)); + if (*state) + { + (*state)->a = NULL; + (*state)->b = NULL; + } + return (*state); +} diff --git a/src/util/is_sorted.c b/src/util/is_sorted.c new file mode 100644 index 0000000..2622d74 --- /dev/null +++ b/src/util/is_sorted.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* is_sorted.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:13:34 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:13:43 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +int is_sorted(t_list *stack) +{ + while (stack->next) + { + if (*(int *) stack->content > *(int *) stack->next->content) + return (0); + stack = stack->next; + } + return (1); +} diff --git a/src/util/lis_indices.c b/src/util/lis_indices.c new file mode 100644 index 0000000..85e91c3 --- /dev/null +++ b/src/util/lis_indices.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* lis_indices.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:12:55 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:13:28 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include + +int *lis_indices(int *lengths, int size, int max_len) +{ + int *indices; + int i; + int j; + + indices = (int *)malloc(sizeof(int) * max_len); + if (!indices) + return (NULL); + i = size - 1; + j = max_len - 1; + while (i >= 0) + { + if (lengths[i] == max_len) + { + indices[j--] = i; + max_len--; + } + i--; + } + return (indices); +} diff --git a/src/util/lis_lengths.c b/src/util/lis_lengths.c new file mode 100644 index 0000000..dff5995 --- /dev/null +++ b/src/util/lis_lengths.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* lis_lengths.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:11:44 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:12:41 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" +#include + +int *lis_lengths(t_list *stack, int size) +{ + int *lengths; + int i; + int j; + + lengths = (int *)malloc(sizeof(int) * size); + if (!lengths) + return (NULL); + i = 0; + while (i < size) + lengths[i++] = 1; + i = 1; + while (i < size) + { + j = 0; + while (j < i) + { + if (*(int *)ft_lstat(stack, j)->content + < *(int *)ft_lstat(stack, i)->content + && lengths[i] < lengths[j] + 1) + lengths[i] = lengths[j] + 1; + j++; + } + i++; + } + return (lengths); +} diff --git a/src/util/longest_incremental_subsequence.c b/src/util/longest_incremental_subsequence.c new file mode 100644 index 0000000..8744c7f --- /dev/null +++ b/src/util/longest_incremental_subsequence.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* longest_incremental_subsequence.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:08:27 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:08:31 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include +#include "push_swap.h" + +int *longest_incremental_subsequence(t_list *stack, int *lis_size) +{ + int size; + int *lengths; + int max_len; + int *indices; + int i; + + size = ft_lstsize(stack); + if (size == 0) + return (NULL); + lengths = lis_lengths(stack, size); + if (!lengths) + return (NULL); + max_len = 0; + i = 0; + while (i < size) + { + if (lengths[i] > max_len) + max_len = lengths[i]; + i++; + } + indices = lis_indices(lengths, size, max_len); + free(lengths); + *lis_size = max_len; + return (indices); +} diff --git a/src/util/print_stack.c b/src/util/print_stack.c new file mode 100644 index 0000000..6795715 --- /dev/null +++ b/src/util/print_stack.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* print_stack.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:06:56 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:08:07 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "push_swap.h" + +void print_stack(const char *name, t_list *stack) +{ + int lst_size; + t_list *node; + + ft_printf("%s |", name); + if (!stack) + { + ft_printf("\n"); + return ; + } + lst_size = ft_lstsize(stack); + while (lst_size > 0) + { + node = ft_lstat(stack, lst_size - 1); + ft_printf("%d ", *((int *) node->content)); + lst_size--; + } + ft_printf("\n"); +} diff --git a/src/util/print_state.c b/src/util/print_state.c new file mode 100644 index 0000000..051d3f7 --- /dev/null +++ b/src/util/print_state.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* print_state.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/11/24 15:07:34 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/11/24 15:07:35 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "push_swap.h" + +void print_state(t_state *state) +{ + ft_printf("\n"); + print_stack("A", state->a); + print_stack("B", state->b); + ft_printf("===================\n"); +} diff --git a/tester.sh b/tester.sh old mode 100644 new mode 100755 index 2bc74e3..b671860 --- a/tester.sh +++ b/tester.sh @@ -3,7 +3,7 @@ # This script is used to test the program run_test_case() { - echo "Test case $1" + # echo "Test case $1" MAX=0 MIN=10000000 AVG=0 @@ -37,22 +37,40 @@ run_test_case() { fi done AVG=$((AVG / ITER)) - echo "Iterations: $ITER" - echo "Max: $MAX" - echo "Min: $MIN" - echo "Average: $AVG" - echo "--------------------------------" - echo "Max Args: $MAX_ARGS" - echo "--------------------------------" - echo "" + # echo -e "$1,\t$AVG,\t$MAX,\t$MIN" + echo "List size: $1 Iterations: $ITER avg: $AVG max: $MAX min: $MIN" + # echo "Max: $MAX" + # echo "Min: $MIN" + # echo "Average: $AVG" + # echo "--------------------------------" + # echo "Max Args: $MAX_ARGS" + # echo "--------------------------------" + # echo "" } run_test_case 1 10 run_test_case 2 10 -run_test_case 3 200 -run_test_case 5 200 -run_test_case 50 100 -run_test_case 100 100 -run_test_case 500 50 +run_test_case 3 10 +run_test_case 4 10 +run_test_case 5 10 +run_test_case 6 10 +run_test_case 7 10 +run_test_case 8 10 +run_test_case 9 10 +run_test_case 10 10 +run_test_case 20 10 +run_test_case 30 10 +run_test_case 40 10 +run_test_case 50 10 +run_test_case 60 10 +run_test_case 70 10 +run_test_case 80 10 +run_test_case 90 10 +run_test_case 100 10 +run_test_case 200 10 +run_test_case 300 10 +run_test_case 400 10 +run_test_case 500 10 +