]> git.dkaiser.de - 42/push_swap.git/commitdiff
Add radixsort
authorDominik Kaiser <dkaiser@2-E-4.42heilbronn.de>
Sat, 13 Apr 2024 13:31:29 +0000 (15:31 +0200)
committerDominik Kaiser <dkaiser@2-E-4.42heilbronn.de>
Sat, 13 Apr 2024 13:31:29 +0000 (15:31 +0200)
Makefile
main.c
sorting.c

index 9a704a1d8128346f48da90a897090de70d1d74cb..5509c89feb4d0c1a3eb1bfcc707a4fd3f048e6d3 100644 (file)
--- 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 84cc7adc21563ee02491c12c4bfeeba5c795fc66..1476fb101b8031a760e08d8bbc03567e7fc4086a 100644 (file)
--- 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);
 }
index 3624ae52d8d70abd8e90e4fa6e16e8499bd329b9..135a94d1884da6c74f88392eac6b02c9272d2463 100644 (file)
--- 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);
 }