summaryrefslogtreecommitdiff
path: root/sorting.c
blob: 6790fa2e583c13ffc070c9f6418e142f5ede4f18 (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
50
51
52
53
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   sorting.c                                          :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/04/13 15:04:19 by dkaiser           #+#    #+#             */
/*   Updated: 2024/04/13 19:29:05 by dkaiser          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

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

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

	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);
}

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

	bit = 0;
	max_bits = 9;
	while (bit < max_bits)
	{
		radixsort_step(stack_a, stack_b, cmds, bit);
		bit++;
	}
}

void	stack_sort(t_list **stack_a, t_list **stack_b, t_list **cmds)
{
	stack_radixsort(stack_a, stack_b, cmds);
}