g vire le bit shift c mieu
This commit is contained in:
parent
7ccda635a2
commit
c4efdc8ff1
4
Makefile
4
Makefile
@ -6,11 +6,11 @@
|
|||||||
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
|
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2022/11/28 16:34:37 by cchauvet #+# #+# #
|
# 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}
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
|
||||||
|
BIN
ft_get_bit_max.o
BIN
ft_get_bit_max.o
Binary file not shown.
BIN
ft_get_bit_min.o
BIN
ft_get_bit_min.o
Binary file not shown.
@ -6,34 +6,22 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/29 14:41:08 by cchauvet #+# #+# */
|
/* 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"
|
#include "pushswap.h"
|
||||||
|
|
||||||
int ft_is_sortee(int *in)
|
int ft_is_sorted(unsigned int *tab)
|
||||||
{
|
{
|
||||||
unsigned int *tab;
|
int i;
|
||||||
unsigned int prev;
|
|
||||||
unsigned int i;
|
|
||||||
int shift;
|
|
||||||
|
|
||||||
shift = ft_bit_finder_right(ft_get_bit_min(in), 1);
|
i = 0;
|
||||||
tab = (unsigned int *) in;
|
while (tab[i] != STOP_VALUE)
|
||||||
prev = tab[0];
|
|
||||||
i = 1;
|
|
||||||
while (in[i] != -1)
|
|
||||||
{
|
{
|
||||||
if (tab[i] >> shift != (prev >> shift) - 1)
|
if (tab[1] != 0)
|
||||||
return (0);
|
return (0);
|
||||||
prev = tab[i];
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ft_is_sorted(int *in)
|
|
||||||
{
|
|
||||||
return (ft_bit_finder_right(ft_get_bit_min(in), 1) == 31);
|
|
||||||
}
|
|
||||||
|
BIN
ft_is_sorted.o
BIN
ft_is_sorted.o
Binary file not shown.
8
ft_p.c
8
ft_p.c
@ -6,13 +6,13 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/09 16:38:34 by cchauvet #+# #+# */
|
/* 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"
|
#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_src;
|
||||||
int size_dst;
|
int size_dst;
|
||||||
@ -26,13 +26,13 @@ void ft_p(int *tab_src, int *tab_dst)
|
|||||||
tab_src[size_src - 1] = -1;
|
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_p(tab_a, tab_b);
|
||||||
ft_putstr("pb\n");
|
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_p(tab_b, tab_a);
|
||||||
ft_putstr("pa\n");
|
ft_putstr("pa\n");
|
||||||
|
12
ft_r.c
12
ft_r.c
@ -6,31 +6,31 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/28 16:50:03 by cchauvet #+# #+# */
|
/* 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"
|
#include "pushswap.h"
|
||||||
|
|
||||||
static void ft_r(int *tab)
|
static void ft_r(unsigned int *tab)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (tab[i + 1] != -1)
|
while (tab[i + 1] != STOP_VALUE)
|
||||||
{
|
{
|
||||||
ft_swap(tab + i, tab + i + 1);
|
ft_swap(tab + i, tab + i + 1);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_ra(int *tab)
|
void ft_ra(unsigned int *tab)
|
||||||
{
|
{
|
||||||
ft_r(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_r(tab);
|
||||||
ft_putstr("rb\n");
|
ft_putstr("rb\n");
|
||||||
|
14
ft_radix.c
14
ft_radix.c
@ -6,26 +6,24 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/15 18:16:31 by cchauvet #+# #+# */
|
/* 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"
|
#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 tab_index;
|
||||||
int loop_index;
|
int loop_index;
|
||||||
int shift;
|
|
||||||
|
|
||||||
while (!ft_is_sorted(tab_a))
|
while (!ft_is_sorted(tab_a))
|
||||||
{
|
{
|
||||||
tab_index = ft_tablen(tab_a) - 1;
|
tab_index = ft_tablen(tab_a) - 1;
|
||||||
loop_index = tab_index;
|
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);
|
ft_pb(tab_a, tab_b);
|
||||||
tab_index--;
|
tab_index--;
|
||||||
@ -34,11 +32,11 @@ void ft_radix_sort(int *tab_a, int *tab_b)
|
|||||||
ft_ra(tab_a);
|
ft_ra(tab_a);
|
||||||
loop_index--;
|
loop_index--;
|
||||||
}
|
}
|
||||||
while (tab_b[0] != -1)
|
while (tab_b[0] != STOP_VALUE)
|
||||||
ft_pa(tab_a, tab_b);
|
ft_pa(tab_a, tab_b);
|
||||||
tab_index = ft_tablen(tab_a);
|
tab_index = ft_tablen(tab_a);
|
||||||
while (--tab_index >= 0 && !ft_is_sorted(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_a);
|
||||||
free(tab_b);
|
free(tab_b);
|
||||||
|
BIN
ft_radix.o
BIN
ft_radix.o
Binary file not shown.
51
ft_sort.c
51
ft_sort.c
@ -6,65 +6,60 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */
|
/* 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"
|
#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 i;
|
||||||
int *out;
|
unsigned int *out;
|
||||||
int max_index;
|
int max_index;
|
||||||
int shift;
|
|
||||||
|
|
||||||
out = malloc(sizeof(int) * (size + 1));
|
out = malloc(sizeof(int) * (size + 1));
|
||||||
if (out == NULL)
|
if (out == NULL)
|
||||||
{
|
{
|
||||||
free(tab);
|
free(tab_in);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
shift = ft_bit_finder_left(size, 1);
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < size)
|
while (i < size)
|
||||||
{
|
{
|
||||||
max_index = ft_get_max_index(tab, size);
|
max_index = ft_get_max_index(tab_in, size);
|
||||||
out[max_index] = (size - i) << shift;
|
out[max_index] = (size - i);
|
||||||
tab[max_index] = INT_MIN;
|
tab_in[max_index] = INT_MIN;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
out[size] = -1;
|
out[size] = STOP_VALUE;
|
||||||
free (tab);
|
free (tab_in);
|
||||||
return (out);
|
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;
|
*tab_b = malloc(sizeof(int) * (size + 1));
|
||||||
int *out_a;
|
*tab_a = ft_values_simplifier(tab_in, size);
|
||||||
|
if (tab_a == NULL || tab_b == NULL)
|
||||||
out_b = malloc(sizeof(int) * (size + 1));
|
|
||||||
out_a = ft_values_simplifier(*tab_a, size);
|
|
||||||
if (out_a == NULL || out_b == NULL)
|
|
||||||
{
|
{
|
||||||
free(*tab_a);
|
free(tab_in);
|
||||||
free(out_a);
|
free(tab_a);
|
||||||
free(out_b);
|
free(tab_b);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
out_b[0] = -1;
|
*(tab_b)[0] = STOP_VALUE;
|
||||||
*tab_a = out_a;
|
return (*tab_a);
|
||||||
*tab_b = out_b;
|
|
||||||
return (out_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;
|
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 ;
|
return ;
|
||||||
ft_radix_sort(tab_a, tab_b);
|
ft_radix_sort(tab_a, tab_b);
|
||||||
}
|
}
|
||||||
|
@ -6,15 +6,15 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/08 20:16:30 by cchauvet #+# #+# */
|
/* 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"
|
#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;
|
temp = *a;
|
||||||
*a = *b;
|
*a = *b;
|
||||||
|
@ -6,18 +6,18 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/23 16:15:15 by cchauvet #+# #+# */
|
/* 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"
|
#include "pushswap.h"
|
||||||
|
|
||||||
int ft_tablen(int *tab)
|
int ft_tablen(unsigned int *tab)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (tab[i] != -1)
|
while (tab[i] != STOP_VALUE)
|
||||||
i++;
|
i++;
|
||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
BIN
ft_tablen.o
BIN
ft_tablen.o
Binary file not shown.
23
pushswap.h
23
pushswap.h
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/08 18:19:44 by cchauvet #+# #+# */
|
/* 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 <unistd.h>
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
# include <limits.h>
|
# include <limits.h>
|
||||||
|
# define STOP_VALUE 4294967295
|
||||||
|
|
||||||
int ft_isnum(char *str);
|
int ft_isnum(char *str);
|
||||||
int ft_atoi(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_get_min_index(int *tab, int len);
|
int ft_get_min_index(int *tab, int len);
|
||||||
|
|
||||||
int ft_tablen(int *tab);
|
int ft_tablen(unsigned int *tab);
|
||||||
int ft_is_sorted(int *in);
|
int ft_is_sorted(unsigned int *in);
|
||||||
|
|
||||||
int ft_bit_finder_left(int big, int tofind);
|
void ft_radix_sort(unsigned int *tab_a, unsigned int *tab_b);
|
||||||
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_swap(int *a, int *b);
|
void ft_swap(unsigned int *a, unsigned int *b);
|
||||||
void ft_pa(int *tab_a, int *tab_b);
|
void ft_pa(unsigned int *tab_a, unsigned int *tab_b);
|
||||||
void ft_pb(int *tab_a, int *tab_b);
|
void ft_pb(unsigned int *tab_a, unsigned int *tab_b);
|
||||||
void ft_ra(int *tab_a);
|
void ft_ra(unsigned int *tab_a);
|
||||||
void ft_rb(int *tab_b);
|
void ft_rb(unsigned int *tab_b);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user