summaryrefslogtreecommitdiff
path: root/sorting.c
blob: ff825b6eabe1bb234df39ae62c386639b8fab884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   sorting.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/04/13 15:04:19 by dkaiser           #+#    #+#             */
/*   Updated: 2024/04/13 17:43:18 by dkaiser          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft/libft.h"
#include "push_swap.h"



static void stack_radixsort(t_list **stack_a, t_list **stack_b, t_list **cmds)
{
    int bit;
    int max;
    int i;
    int max_bits;

    bit = 0;
    max_bits = 9;

    while (bit < max_bits)
    {
        i = 0;
        max = ft_lstsize(*stack_a);
        while (i < max)
        {
            if (*stack_a && (*(int *)(*stack_a)->content >> bit) % 2 == 0)
                run_command(stack_a, stack_b, cmds, PB);
            else if ((*stack_a)->next)
                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);
}