summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2024-04-13 15:31:29 +0200
committerDominik Kaiser2024-04-13 15:31:29 +0200
commit9674332f03f434534017c0f70f3c213fa4ebb2bf (patch)
tree09c455e40cb2ca91021ced93ceb8e827dba4ba54
parent9585e1d336c6a1581b335798762172314de1fefb (diff)
downloadpush_swap-9674332f03f434534017c0f70f3c213fa4ebb2bf.tar.gz
push_swap-9674332f03f434534017c0f70f3c213fa4ebb2bf.zip
Add radixsort
-rw-r--r--Makefile2
-rw-r--r--main.c17
-rw-r--r--sorting.c30
3 files changed, 36 insertions, 13 deletions
diff --git a/Makefile b/Makefile
index 9a704a1..5509c89 100644
--- a/Makefile
+++ b/Makefile
@@ -5,7 +5,7 @@ LIBFT = libft
CC = cc
CFLAGS = -Wall -Wextra -Werror
-SRC_FILES = main.c input_handling.c stack_utils.c command_handling.c
+SRC_FILES = main.c input_handling.c stack_utils.c command_handling.c sorting.c
OBJ_FILES = $(SRC_FILES:%.c=%.o)
LIB_DIR = $(LIBFT)
diff --git a/main.c b/main.c
index 84cc7ad..1476fb1 100644
--- a/main.c
+++ b/main.c
@@ -6,7 +6,7 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/12 17:03:30 by dkaiser #+# #+# */
-/* Updated: 2024/04/13 15:04:10 by dkaiser ### ########.fr */
+/* Updated: 2024/04/13 15:31:05 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,27 +20,24 @@ static void print_content(void *content)
int main(int argc, char *argv[])
{
t_list *stack_a;
- t_list *stack_b;
- t_list *pscmds;
+ t_list *stack_b;
+ t_list *pscmds;
stack_a = create_stack(argc, argv);
if (!stack_a)
{
ft_putendl_fd("Error", 2);
- return 1;
+ return (1);
}
stack_b = NULL;
pscmds = NULL;
-
stack_sort(&stack_a, &stack_b, &pscmds);
-
-
// TODO: Optimize commands
+ print_commands(pscmds);
ft_printf("A:");
ft_lstiter(stack_a, print_content);
ft_printf("\nB:");
ft_lstiter(stack_b, print_content);
- ft_printf("\n\nCMDS:\n");
- print_commands(pscmds);
- return 0;
+ ft_printf("\n");
+ return (0);
}
diff --git a/sorting.c b/sorting.c
index 3624ae5..135a94d 100644
--- a/sorting.c
+++ b/sorting.c
@@ -6,13 +6,39 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/13 15:04:19 by dkaiser #+# #+# */
-/* Updated: 2024/04/13 15:06:33 by dkaiser ### ########.fr */
+/* Updated: 2024/04/13 15:17:26 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
+#include "libft/libft.h"
#include "push_swap.h"
-void stack_sort(t_list **stack_a, t_list **stack_b, t_list **cmds)
+static void stack_radixsort(t_list **stack_a, t_list **stack_b, t_list **cmds)
{
+ int bit;
+ int max;
+ int i;
+
+ bit = 0;
+ while (bit < 9)
+ {
+ i = 0;
+ max = ft_lstsize(*stack_a);
+ while (i < max)
+ {
+ if ((*(int *)(*stack_a)->content >> bit) % 2 == 0)
+ run_command(stack_a, stack_b, cmds, PB);
+ else
+ run_command(stack_a, stack_b, cmds, RA);
+ i++;
+ }
+ while (*stack_b)
+ run_command(stack_a, stack_b, cmds, PA);
+ bit++;
+ }
+}
+void stack_sort(t_list **stack_a, t_list **stack_b, t_list **cmds)
+{
+ stack_radixsort(stack_a, stack_b, cmds);
}