g vire le bit shift c mieu

This commit is contained in:
Camille Chauvet 2022-12-01 17:15:17 +01:00
parent 7ccda635a2
commit c4efdc8ff1
19 changed files with 65 additions and 87 deletions

View File

@ -6,11 +6,11 @@
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/11/28 16:34:37 by cchauvet #+# #+# #
# Updated: 2022/11/29 16:23:42 by cchauvet ### ########.fr #
# Updated: 2022/12/01 16:54:34 by cchauvet ### ########.fr #
# #
# **************************************************************************** #
SRCS = ft_swap.c ft_putstr.c ft_p.c ft_sort.c ft_tablen.c ft_get_max.c ft_get_min.c main.c ft_atoi.c ft_bit_finder.c ft_radix.c ft_isnum.c ft_get_bit_max.c ft_r.c ft_is_sorted.c ft_get_bit_min.c
SRCS = ft_swap.c ft_putstr.c ft_p.c ft_sort.c ft_tablen.c main.c ft_atoi.c ft_radix.c ft_isnum.c ft_r.c ft_is_sorted.c ft_get_max.c
OBJS = ${SRCS:.c=.o}

Binary file not shown.

Binary file not shown.

View File

@ -6,34 +6,22 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/29 14:41:08 by cchauvet #+# #+# */
/* Updated: 2022/11/30 20:09:57 by cchauvet ### ########.fr */
/* Updated: 2022/12/01 17:03:47 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
int ft_is_sortee(int *in)
int ft_is_sorted(unsigned int *tab)
{
unsigned int *tab;
unsigned int prev;
unsigned int i;
int shift;
int i;
shift = ft_bit_finder_right(ft_get_bit_min(in), 1);
tab = (unsigned int *) in;
prev = tab[0];
i = 1;
while (in[i] != -1)
i = 0;
while (tab[i] != STOP_VALUE)
{
if (tab[i] >> shift != (prev >> shift) - 1)
if (tab[1] != 0)
return (0);
prev = tab[i];
i++;
}
return (1);
}
int ft_is_sorted(int *in)
{
return (ft_bit_finder_right(ft_get_bit_min(in), 1) == 31);
}

Binary file not shown.

8
ft_p.c
View File

@ -6,13 +6,13 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 16:38:34 by cchauvet #+# #+# */
/* Updated: 2022/11/29 18:13:28 by cchauvet ### ########.fr */
/* Updated: 2022/12/01 16:17:17 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
void ft_p(int *tab_src, int *tab_dst)
void ft_p(unsigned *tab_src, unsigned *tab_dst)
{
int size_src;
int size_dst;
@ -26,13 +26,13 @@ void ft_p(int *tab_src, int *tab_dst)
tab_src[size_src - 1] = -1;
}
void ft_pb(int *tab_a, int *tab_b)
void ft_pb(unsigned int *tab_a, unsigned int *tab_b)
{
ft_p(tab_a, tab_b);
ft_putstr("pb\n");
}
void ft_pa(int *tab_a, int *tab_b)
void ft_pa(unsigned int *tab_a, unsigned int *tab_b)
{
ft_p(tab_b, tab_a);
ft_putstr("pa\n");

BIN
ft_p.o

Binary file not shown.

12
ft_r.c
View File

@ -6,31 +6,31 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/28 16:50:03 by cchauvet #+# #+# */
/* Updated: 2022/11/29 14:39:23 by cchauvet ### ########.fr */
/* Updated: 2022/12/01 17:10:17 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
static void ft_r(int *tab)
static void ft_r(unsigned int *tab)
{
int i;
i = 0;
while (tab[i + 1] != -1)
while (tab[i + 1] != STOP_VALUE)
{
ft_swap(tab + i, tab + i + 1);
i++;
}
}
void ft_ra(int *tab)
void ft_ra(unsigned int *tab)
{
ft_r(tab);
ft_putstr("ra\n");
ft_putstr("rra\n");
}
void ft_rb(int *tab)
void ft_rb(unsigned int *tab)
{
ft_r(tab);
ft_putstr("rb\n");

BIN
ft_r.o

Binary file not shown.

View File

@ -6,26 +6,24 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/15 18:16:31 by cchauvet #+# #+# */
/* Updated: 2022/11/30 20:06:39 by cchauvet ### ########.fr */
/* Updated: 2022/12/01 17:13:35 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
void ft_radix_sort(int *tab_a, int *tab_b)
void ft_radix_sort(unsigned *tab_a, unsigned *tab_b)
{
int tab_index;
int loop_index;
int shift;
while (!ft_is_sorted(tab_a))
{
tab_index = ft_tablen(tab_a) - 1;
loop_index = tab_index;
shift = ft_bit_finder_right(ft_get_bit_min(tab_a), 1);
while (loop_index > 0 && !ft_is_sorted(tab_a))
while (loop_index >= 0 && !ft_is_sorted(tab_a))
{
if (tab_a[tab_index] >> shift & 1)
if (tab_a[tab_index] % 2 == 1)
{
ft_pb(tab_a, tab_b);
tab_index--;
@ -34,11 +32,11 @@ void ft_radix_sort(int *tab_a, int *tab_b)
ft_ra(tab_a);
loop_index--;
}
while (tab_b[0] != -1)
while (tab_b[0] != STOP_VALUE)
ft_pa(tab_a, tab_b);
tab_index = ft_tablen(tab_a);
while (--tab_index >= 0 && !ft_is_sorted(tab_a))
tab_a[tab_index] = tab_a[tab_index] << 1;
tab_a[tab_index] = tab_a[tab_index] >> 1;
}
free(tab_a);
free(tab_b);

Binary file not shown.

View File

@ -6,65 +6,60 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */
/* Updated: 2022/11/29 16:19:48 by cchauvet ### ########.fr */
/* Updated: 2022/12/01 17:00:04 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
static int *ft_values_simplifier(int *tab, int size)
static unsigned int *ft_values_simplifier(int *tab_in, int size)
{
int i;
int *out;
int max_index;
int shift;
int i;
unsigned int *out;
int max_index;
out = malloc(sizeof(int) * (size + 1));
if (out == NULL)
{
free(tab);
free(tab_in);
return (NULL);
}
shift = ft_bit_finder_left(size, 1);
i = 0;
while (i < size)
{
max_index = ft_get_max_index(tab, size);
out[max_index] = (size - i) << shift;
tab[max_index] = INT_MIN;
max_index = ft_get_max_index(tab_in, size);
out[max_index] = (size - i);
tab_in[max_index] = INT_MIN;
i++;
}
out[size] = -1;
free (tab);
out[size] = STOP_VALUE;
free (tab_in);
return (out);
}
static int *ft_tab_init(int **tab_a, int **tab_b, int size)
static unsigned int *ft_tab_init(int *tab_in, int size, unsigned **tab_a, unsigned **tab_b)
{
int *out_b;
int *out_a;
out_b = malloc(sizeof(int) * (size + 1));
out_a = ft_values_simplifier(*tab_a, size);
if (out_a == NULL || out_b == NULL)
*tab_b = malloc(sizeof(int) * (size + 1));
*tab_a = ft_values_simplifier(tab_in, size);
if (tab_a == NULL || tab_b == NULL)
{
free(*tab_a);
free(out_a);
free(out_b);
free(tab_in);
free(tab_a);
free(tab_b);
return (NULL);
}
out_b[0] = -1;
*tab_a = out_a;
*tab_b = out_b;
return (out_a);
*(tab_b)[0] = STOP_VALUE;
return (*tab_a);
}
void ft_sort(int *tab_a, int size_a)
void ft_sort(int *tab_in, int size_a)
{
int *tab_b;
unsigned int *tab_a;
unsigned int *tab_b;
tab_b = NULL;
if (ft_tab_init(&tab_a, &tab_b, size_a) == NULL)
tab_a = NULL;
if (ft_tab_init(tab_in, size_a, &tab_a, &tab_b) == NULL)
return ;
ft_radix_sort(tab_a, tab_b);
}

BIN
ft_sort.o

Binary file not shown.

View File

@ -6,15 +6,15 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 20:16:30 by cchauvet #+# #+# */
/* Updated: 2022/11/08 20:17:58 by cchauvet ### ########.fr */
/* Updated: 2022/12/01 16:49:55 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
void ft_swap(int *a, int *b)
void ft_swap(unsigned int *a, unsigned int *b)
{
int temp;
unsigned int temp;
temp = *a;
*a = *b;

BIN
ft_swap.o

Binary file not shown.

View File

@ -6,18 +6,18 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/23 16:15:15 by cchauvet #+# #+# */
/* Updated: 2022/11/23 16:23:26 by cchauvet ### ########.fr */
/* Updated: 2022/12/01 16:51:50 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
int ft_tablen(int *tab)
int ft_tablen(unsigned int *tab)
{
int i;
i = 0;
while (tab[i] != -1)
while (tab[i] != STOP_VALUE)
i++;
return (i);
}

Binary file not shown.

BIN
push_swap

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 18:19:44 by cchauvet #+# #+# */
/* Updated: 2022/11/30 13:30:55 by cchauvet ### ########.fr */
/* Updated: 2022/12/01 16:51:27 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
# include <unistd.h>
# include <stdlib.h>
# include <limits.h>
# define STOP_VALUE 4294967295
int ft_isnum(char *str);
int ft_atoi(char *str);
@ -27,19 +28,15 @@ int ft_get_max_index(int *tab, int len);
int ft_get_min_index(int *tab, int len);
int ft_get_min_index(int *tab, int len);
int ft_tablen(int *tab);
int ft_is_sorted(int *in);
int ft_tablen(unsigned int *tab);
int ft_is_sorted(unsigned int *in);
int ft_bit_finder_left(int big, int tofind);
int ft_bit_finder_right(int big, int tofind);
int ft_get_bit_max(int *tab);
int ft_get_bit_min(int *tab);
void ft_radix_sort(int *tab_a, int *tab_b);
void ft_radix_sort(unsigned int *tab_a, unsigned int *tab_b);
void ft_swap(int *a, int *b);
void ft_pa(int *tab_a, int *tab_b);
void ft_pb(int *tab_a, int *tab_b);
void ft_ra(int *tab_a);
void ft_rb(int *tab_b);
void ft_swap(unsigned int *a, unsigned int *b);
void ft_pa(unsigned int *tab_a, unsigned int *tab_b);
void ft_pb(unsigned int *tab_a, unsigned int *tab_b);
void ft_ra(unsigned int *tab_a);
void ft_rb(unsigned int *tab_b);
#endif