summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/errors.c89
-rw-r--r--src/free.c44
-rw-r--r--src/ft_lstmax.c33
-rw-r--r--src/ft_lstmin.c33
-rw-r--r--src/helper.c66
-rw-r--r--src/main.c132
-rw-r--r--src/pstack.c32
-rw-r--r--src/push.c47
-rw-r--r--src/rotate.c58
-rw-r--r--src/rotate_stacks.c96
-rw-r--r--src/rrotate.c63
-rw-r--r--src/sort.c115
-rw-r--r--src/sort_five.c35
-rw-r--r--src/sort_three.c48
-rw-r--r--src/sort_two.c32
-rw-r--r--src/swap.c56
16 files changed, 979 insertions, 0 deletions
diff --git a/src/errors.c b/src/errors.c
new file mode 100644
index 0000000..63e661e
--- /dev/null
+++ b/src/errors.c
@@ -0,0 +1,89 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+#include "../include/push_swap.h"
+#include <limits.h>
+
+void puterror(t_list *src)
+{
+ if (src)
+ free_stack(src);
+ write(1, "Error\n", 6);
+ exit(1);
+}
+
+int is_dup(t_list *stack)
+{
+ t_list *first;
+ t_list *second;
+
+ first = stack;
+ second = stack->next;
+ while (first)
+ {
+ second = first->next;
+ while (second)
+ {
+ if (first->value == second->value)
+ return (1);
+ second = second->next;
+ }
+ first = first->next;
+ }
+ return (0);
+}
+
+int is_limit_int(t_list *stack)
+{
+ while (stack)
+ {
+ if (stack->value > INT_MAX || stack->value < INT_MIN)
+ return (1);
+ stack = stack->next;
+ }
+ return (0);
+}
+
+int is_valid_int(char **array)
+{
+ int j;
+ int i;
+
+ j = 0;
+ while (array[j])
+ {
+ i = 0;
+ while (array[j][i])
+ {
+ if (array[j][i] == '-' && i == 0)
+ i++;
+ if (array[j][i] < '0' || array[j][i] > '9')
+ return (0);
+ i++;
+ }
+ j++;
+ }
+ return (1);
+}
+
+void check_errors(t_list *src)
+{
+ if (is_dup(src))
+ puterror(src);
+ if (is_limit_int(src))
+ puterror(src);
+}
diff --git a/src/free.c b/src/free.c
new file mode 100644
index 0000000..e89f83f
--- /dev/null
+++ b/src/free.c
@@ -0,0 +1,44 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+void free_array(char **array)
+{
+ int i;
+
+ i = 0;
+ while (array[i])
+ {
+ free(array[i]);
+ i++;
+ }
+ free(array);
+}
+
+void free_stack(t_list *stack)
+{
+ t_list *tmp;
+
+ while (stack)
+ {
+ tmp = stack->next;
+ free(stack);
+ stack = tmp;
+ }
+}
diff --git a/src/ft_lstmax.c b/src/ft_lstmax.c
new file mode 100644
index 0000000..59b9af3
--- /dev/null
+++ b/src/ft_lstmax.c
@@ -0,0 +1,33 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+t_list *ft_lstmax(t_list *stack)
+{
+ t_list *max;
+
+ max = stack;
+ while (stack)
+ {
+ if (stack->value > max->value)
+ max = stack;
+ stack = stack->next;
+ }
+ return (max);
+}
diff --git a/src/ft_lstmin.c b/src/ft_lstmin.c
new file mode 100644
index 0000000..54cd66f
--- /dev/null
+++ b/src/ft_lstmin.c
@@ -0,0 +1,33 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+t_list *ft_lstmin(t_list *stack)
+{
+ t_list *min;
+
+ min = stack;
+ while (stack)
+ {
+ if (stack->value < min->value)
+ min = stack;
+ stack = stack->next;
+ }
+ return (min);
+}
diff --git a/src/helper.c b/src/helper.c
new file mode 100644
index 0000000..209a1b8
--- /dev/null
+++ b/src/helper.c
@@ -0,0 +1,66 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+int find_smallest(t_list *stack)
+{
+ int smallest;
+ int position;
+
+ smallest = stack->value;
+ position = stack->index;
+ while (stack)
+ {
+ if (stack->value < smallest)
+ {
+ smallest = stack->value;
+ position = stack->index;
+ }
+ stack = stack->next;
+ }
+ return (position);
+}
+
+void move_smallest_value_dst(t_list **src, t_list **dst,
+ int position_smallest_value)
+{
+ int i;
+
+ i = 0;
+ if (position_smallest_value < ft_lstsize(*src))
+ while (i++ < position_smallest_value)
+ ra(src);
+ if (position_smallest_value > ft_lstsize(*src))
+ while (i++ < position_smallest_value)
+ rra(src);
+ pb(src, dst);
+}
+
+void reset_index(t_list *stack)
+{
+ int i;
+
+ i = 0;
+ while (stack->next != NULL)
+ {
+ stack->index = i++;
+ stack = stack->next;
+ }
+ stack->index = i;
+}
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..a00d8c9
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,132 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+#include <stdlib.h>
+
+int is_sorted(t_list *stack)
+{
+ t_list *current;
+ t_list *second;
+// t_list *next;
+
+ current = stack;
+ //next = stack->next;
+ while (current)
+ {
+ second = current->next;
+ while (second)
+ {
+ if (current->value > second->value)
+ return (0);
+ second = second->next;
+ }
+ current = current->next;
+ }
+ return (1);
+}
+
+void *init_stack(char **array)
+{
+ t_list *node;
+ long number;
+ int index_count;
+ t_list *stack;
+
+ stack = NULL;
+ index_count = 0;
+ while (array[index_count])
+ index_count++;
+ while (index_count > 0)
+ {
+ number = ft_atoi(array[index_count - 1]);
+ node = malloc(sizeof(t_list));
+ if (!node)
+ return (NULL);
+ node->value = number;
+ node->index = (index_count - 1);
+ node->next = stack;
+ stack = node;
+ index_count--;
+ }
+ return (stack);
+}
+
+t_list *convert_string(char *s, t_list *src)
+{
+ char **array;
+
+ array = ft_split(s, ' ');
+ if (array == NULL)
+ return (NULL);
+ if (array[0] == NULL)
+ {
+ free_array(array);
+ return (NULL);
+ }
+ if (!is_valid_int(array))
+ {
+ free_array(array);
+ puterror(src);
+ }
+ src = init_stack(array);
+ free_array(array);
+ check_errors(src);
+ return (src);
+}
+
+void select_sort(t_list *src)
+{
+ if (ft_lstsize(src) < 3)
+ sort_two(&src);
+ else if (ft_lstsize(src) < 4)
+ sort_three(&src);
+ else if (ft_lstsize(src) < 6)
+ sort_five(&src);
+ else
+ sort(&src);
+ free_stack(src);
+}
+
+int main(int argc, char **argv)
+{
+ t_list *src;
+
+ src = NULL;
+ if (argc == 1)
+ return (0);
+ if (argc == 2)
+ {
+ src = convert_string(argv[1], src);
+ if (src == NULL)
+ {
+ free(src);
+ return (0);
+ }
+ }
+ else
+ {
+ if (!is_valid_int(&argv[1]))
+ puterror(src);
+ src = init_stack(&argv[1]);
+ check_errors(src);
+ }
+ if (is_sorted(src))
+ return (free_stack(src), 0);
+ select_sort(src);
+}
diff --git a/src/pstack.c b/src/pstack.c
new file mode 100644
index 0000000..c8489f9
--- /dev/null
+++ b/src/pstack.c
@@ -0,0 +1,32 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+void pstack(t_list *stack, char c)
+{
+ t_list *ptr;
+
+ ptr = stack;
+ printf("stack %c:\n", c);
+ while (ptr != NULL)
+ {
+ printf("[%i] %li\n", ptr->index, ptr->value);
+ ptr = ptr->next;
+ }
+}
diff --git a/src/push.c b/src/push.c
new file mode 100644
index 0000000..b15f9e4
--- /dev/null
+++ b/src/push.c
@@ -0,0 +1,47 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+void push(t_list **src, t_list **dst)
+{
+ t_list *head;
+
+ head = *src;
+ *src = head->next;
+ head->next = *dst;
+ *dst = head;
+ if (*src)
+ reset_index(*src);
+ if (*dst)
+ reset_index(*dst);
+}
+
+void pb(t_list **stack_a, t_list **stack_b)
+{
+ push(stack_a, stack_b);
+ write(1, "pb", 2);
+ write(1, "\n", 1);
+}
+
+void pa(t_list **stack_b, t_list **stack_a)
+{
+ push(stack_b, stack_a);
+ write(1, "pa", 2);
+ write(1, "\n", 1);
+}
diff --git a/src/rotate.c b/src/rotate.c
new file mode 100644
index 0000000..7656ac5
--- /dev/null
+++ b/src/rotate.c
@@ -0,0 +1,58 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+void rotate(t_list **stack)
+{
+ t_list *a;
+ t_list *b;
+ t_list *current;
+
+ a = *stack;
+ b = a->next;
+ current = *stack;
+ while (current->next != NULL)
+ current = current->next;
+ current->next = a;
+ a->next = NULL;
+ *stack = b;
+ reset_index(*stack);
+}
+
+void ra(t_list **stack)
+{
+ rotate(stack);
+ write(1, "ra", 2);
+ write(1, "\n", 1);
+}
+
+void rb(t_list **stack)
+{
+ rotate(stack);
+ write(1, "rb", 2);
+ write(1, "\n", 1);
+}
+
+void rr(t_list **stack_a, t_list **stack_b)
+{
+ rotate(stack_a);
+ rotate(stack_b);
+ write(1, "rr", 2);
+ write(1, "\n", 1);
+}
diff --git a/src/rotate_stacks.c b/src/rotate_stacks.c
new file mode 100644
index 0000000..b66c487
--- /dev/null
+++ b/src/rotate_stacks.c
@@ -0,0 +1,96 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+void double_rrr(int position_a, int position_b, t_list **src, t_list **dst)
+{
+ int i;
+
+ while ((ft_lstsize(*src) != position_a) && (ft_lstsize(*dst)) != position_b)
+ {
+ rrr(src, dst);
+ position_a++;
+ position_b++;
+ }
+ i = 0;
+ while (i++ < (ft_lstsize(*dst)) - position_b)
+ rrb(dst);
+ i = 0;
+ while (i++ < (ft_lstsize(*src)) - position_a)
+ rra(src);
+}
+
+void double_rr(int position_a, int position_b, t_list **src, t_list **dst)
+{
+ int i;
+
+ while (position_a && position_b)
+ {
+ rr(src, dst);
+ position_a--;
+ position_b--;
+ }
+ i = 0;
+ while (i++ < position_b)
+ rb(dst);
+ i = 0;
+ while (i++ < position_a)
+ ra(src);
+}
+
+void single_r(int position_a, int position_b, t_list **src, t_list **dst)
+{
+ int i;
+
+ i = 0;
+ if (position_b > ft_lstsize(*dst) / 2)
+ {
+ while (i++ < (ft_lstsize(*dst)) - position_b)
+ rrb(dst);
+ }
+ else
+ {
+ while (i++ < position_b)
+ rb(dst);
+ }
+ i = 0;
+ if (position_a > ft_lstsize(*src) / 2)
+ {
+ while (i++ < (ft_lstsize(*src)) - position_a)
+ rra(src);
+ }
+ else
+ {
+ while (i++ < position_a)
+ ra(src);
+ }
+}
+
+void rotate_stacks(int position_a, int position_b, t_list **src,
+ t_list **dst)
+{
+ if ((position_a > ft_lstsize(*src) / 2) && (position_b > ft_lstsize(*dst)
+ / 2))
+ double_rrr(position_a, position_b, src, dst);
+ else if ((position_a < ft_lstsize(*src) / 2)
+ && (position_b < ft_lstsize(*dst) / 2))
+ double_rr(position_a, position_b, src, dst);
+ else
+ single_r(position_a, position_b, src, dst);
+}
diff --git a/src/rrotate.c b/src/rrotate.c
new file mode 100644
index 0000000..42be064
--- /dev/null
+++ b/src/rrotate.c
@@ -0,0 +1,63 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+void rrotate(t_list **stack)
+{
+ t_list *a;
+// t_list *b;
+ t_list *current;
+ t_list *prev;
+
+ a = *stack;
+// b = a->next;
+ current = *stack;
+ prev = NULL;
+ while (current->next != NULL)
+ {
+ prev = current;
+ current = current->next;
+ }
+ current->next = a;
+ prev->next = NULL;
+ *stack = current;
+ reset_index(*stack);
+}
+
+void rra(t_list **stack)
+{
+ rrotate(stack);
+ write(1, "rra", 3);
+ write(1, "\n", 1);
+}
+
+void rrb(t_list **stack)
+{
+ rrotate(stack);
+ write(1, "rrb", 3);
+ write(1, "\n", 1);
+}
+
+void rrr(t_list **stack_a, t_list **stack_b)
+{
+ rrotate(stack_a);
+ rrotate(stack_b);
+ write(1, "rrr", 3);
+ write(1, "\n", 1);
+}
diff --git a/src/sort.c b/src/sort.c
new file mode 100644
index 0000000..ba3b755
--- /dev/null
+++ b/src/sort.c
@@ -0,0 +1,115 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+int greatest_smallest_value(int element, t_list *stack)
+{
+ int saved;
+
+ saved = (ft_lstmin(stack))->value;
+ while (stack)
+ {
+ if (stack->value <= element && stack->value > saved)
+ saved = stack->value;
+ stack = stack->next;
+ }
+ return (saved);
+}
+
+int find_position_stack(int element, t_list *stack)
+{
+ int position;
+
+ position = ft_lstmax(stack)->index;
+ while (stack)
+ {
+ if (element >= stack->value
+ && stack->value >= greatest_smallest_value(element, stack))
+ {
+ position = stack->index;
+ return (position);
+ }
+ stack = stack->next;
+ }
+ return (position);
+}
+
+int find_cheapest_element(t_list *src, t_list *dst)
+{
+ int index;
+ int cost;
+ int node_cost;
+
+ cost = 0;
+ index = src->index;
+ while (src)
+ {
+ node_cost = src->index;
+ node_cost += find_position_stack(src->value, dst);
+ if (!cost)
+ cost = node_cost;
+ else if (cost && (node_cost < cost))
+ {
+ cost = node_cost;
+ index = src->index;
+ }
+ src = src->next;
+ }
+ return (index);
+}
+
+void push_and_sort(t_list **src, t_list **dst)
+{
+ int position_a;
+ int position_b;
+ t_list *cheapst_element;
+
+ while (*src)
+ {
+ position_a = find_cheapest_element(*src, *dst);
+ cheapst_element = *src;
+ while (cheapst_element->index < position_a)
+ cheapst_element = cheapst_element->next;
+ position_b = find_position_stack(cheapst_element->value, *dst);
+ rotate_stacks(position_a, position_b, src, dst);
+ pb(src, dst);
+ }
+}
+
+void sort(t_list **src)
+{
+ t_list *dst;
+
+ dst = NULL;
+ pb(src, &dst);
+ pb(src, &dst);
+ push_and_sort(src, &dst);
+ while (dst)
+ pa(&dst, src);
+ if ((ft_lstmin(*src))->index < (ft_lstsize(*src) / 2))
+ {
+ while ((*src)->value != ft_lstmin(*src)->value)
+ ra(src);
+ }
+ else
+ {
+ while ((*src)->value != ft_lstmin(*src)->value)
+ rra(src);
+ }
+}
diff --git a/src/sort_five.c b/src/sort_five.c
new file mode 100644
index 0000000..e460216
--- /dev/null
+++ b/src/sort_five.c
@@ -0,0 +1,35 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+void sort_five(t_list **src)
+{
+ int position_smallest_value;
+ t_list *dst;
+
+ dst = NULL;
+ while (ft_lstsize(*src) > 3)
+ {
+ position_smallest_value = find_smallest(*src);
+ move_smallest_value_dst(src, &dst, position_smallest_value);
+ }
+ sort_three(src);
+ while (dst)
+ pa(&dst, src);
+}
diff --git a/src/sort_three.c b/src/sort_three.c
new file mode 100644
index 0000000..d87e5cb
--- /dev/null
+++ b/src/sort_three.c
@@ -0,0 +1,48 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+void sort_three(t_list **stack)
+{
+ t_list *node;
+ int first;
+ int second;
+ int third;
+
+ node = *stack;
+ first = node->value;
+ second = node->next->value;
+ third = node->next->next->value;
+ if ((first > second) && (first < third))
+ sa(stack);
+ else if ((first > second) && (first > third))
+ {
+ sa(stack);
+ sort_three(stack);
+ }
+ else if ((third < second) && (third < first))
+ rra(stack);
+ else if ((third < second) && (third > first))
+ {
+ rra(stack);
+ sort_three(stack);
+ }
+ else if ((third < second) && (third == first))
+ rra(stack);
+}
diff --git a/src/sort_two.c b/src/sort_two.c
new file mode 100644
index 0000000..b9588be
--- /dev/null
+++ b/src/sort_two.c
@@ -0,0 +1,32 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+void sort_two(t_list **stack)
+{
+ t_list *node;
+ int first;
+ int second;
+
+ node = *stack;
+ first = node->value;
+ second = node->next->value;
+ if ((first > second))
+ sa(stack);
+}
diff --git a/src/swap.c b/src/swap.c
new file mode 100644
index 0000000..4bfc937
--- /dev/null
+++ b/src/swap.c
@@ -0,0 +1,56 @@
+/*
+sort_stack Copyright (C) 2026 yctct
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+
+#include "../include/push_swap.h"
+
+void swap(t_list **stack)
+{
+ t_list *a;
+ t_list *b;
+ //t_list *ptr;
+
+ a = *stack;
+ b = a->next;
+ //ptr = a->next;
+ a->next = b->next;
+ b->next = a;
+ *stack = b;
+ reset_index(*stack);
+}
+
+void sa(t_list **stack)
+{
+ swap(stack);
+ write(1, "sa", 2);
+ write(1, "\n", 1);
+}
+
+void sb(t_list **stack)
+{
+ swap(stack);
+ write(1, "sb", 2);
+ write(1, "\n", 1);
+}
+
+void ss(t_list **stack_a, t_list **stack_b)
+{
+ swap(stack_a);
+ swap(stack_b);
+ write(1, "ss", 2);
+ write(1, "\n", 1);
+}