I think It work
This commit is contained in:
parent
f4bd2c9432
commit
36a1e52502
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/28 17:17:40 by cchauvet ### ########.fr #
|
# Updated: 2022/11/29 16:23:42 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
|
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
|
||||||
|
|
||||||
OBJS = ${SRCS:.c=.o}
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
|
||||||
|
@ -6,18 +6,29 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/23 17:31:10 by cchauvet #+# #+# */
|
/* Created: 2022/11/23 17:31:10 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/11/23 18:35:41 by cchauvet ### ########.fr */
|
/* Updated: 2022/11/29 15:06:00 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "pushswap.h"
|
#include "pushswap.h"
|
||||||
|
|
||||||
int ft_bit_finder(int big, int tofind)
|
int ft_bit_finder_left(int big, int tofind)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = (int) sizeof(int) * 8 - 1;
|
i = (int) sizeof(int) * 8 - 1;
|
||||||
while (i > 0 && ((big >> i) & 1) != tofind)
|
while (i != 0 && ((big >> i) & 1) != tofind)
|
||||||
i--;
|
i--;
|
||||||
return (sizeof(int) * 8 - i - 1);
|
return (sizeof(int) * 8 - i - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int ft_bit_finder_right(int big, int tofind)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i != ((int) sizeof(int) * 8 - 1) && ((big << i) & 1) != tofind)
|
||||||
|
i++;
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
BIN
ft_bit_finder.o
Normal file
BIN
ft_bit_finder.o
Normal file
Binary file not shown.
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/28 16:22:14 by cchauvet #+# #+# */
|
/* Created: 2022/11/28 16:22:14 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/11/28 17:56:32 by cchauvet ### ########.fr */
|
/* Updated: 2022/11/29 15:10:54 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ int ft_get_bit_max(int *tab)
|
|||||||
i = 1;
|
i = 1;
|
||||||
while (tab[i] != -1)
|
while (tab[i] != -1)
|
||||||
{
|
{
|
||||||
if (ft_bit_finder(tab[i], 1) < ft_bit_finder(max, 1))
|
if (ft_bit_finder_left(tab[i], 1) > ft_bit_finder_left(max, 1))
|
||||||
max = tab[i];
|
max = tab[i];
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
BIN
ft_get_bit_max.o
Normal file
BIN
ft_get_bit_max.o
Normal file
Binary file not shown.
31
ft_get_bit_min.c
Normal file
31
ft_get_bit_min.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_get_bit_min.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/28 16:22:14 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/29 15:10:03 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
int ft_get_bit_min(int *tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int min;
|
||||||
|
|
||||||
|
if (tab[0] == -1)
|
||||||
|
return (INT_MIN);
|
||||||
|
min = tab[0];
|
||||||
|
i = 1;
|
||||||
|
while (tab[i] != -1)
|
||||||
|
{
|
||||||
|
if (ft_bit_finder_right(tab[i], 1) < ft_bit_finder_right(min, 1))
|
||||||
|
min = tab[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (min);
|
||||||
|
}
|
BIN
ft_get_bit_min.o
Normal file
BIN
ft_get_bit_min.o
Normal file
Binary file not shown.
BIN
ft_get_max.o
Normal file
BIN
ft_get_max.o
Normal file
Binary file not shown.
BIN
ft_get_min.o
Normal file
BIN
ft_get_min.o
Normal file
Binary file not shown.
34
ft_is_sorted.c
Normal file
34
ft_is_sorted.c
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_is_sorted.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/29 14:41:08 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/11/29 19:02:34 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
int ft_is_sorted(int *in)
|
||||||
|
{
|
||||||
|
unsigned int *tab;
|
||||||
|
unsigned int prev;
|
||||||
|
unsigned int i;
|
||||||
|
int shift;
|
||||||
|
|
||||||
|
shift = ft_bit_finder_right(ft_get_bit_min(in), 1) - 1;
|
||||||
|
tab = (unsigned int *) in;
|
||||||
|
prev = tab[0];
|
||||||
|
i = 1;
|
||||||
|
while (in[i] != -1)
|
||||||
|
{
|
||||||
|
if (tab[i] >> shift != (prev >> shift) - 1)
|
||||||
|
return (0);
|
||||||
|
prev = tab[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
BIN
ft_is_sorted.o
Normal file
BIN
ft_is_sorted.o
Normal file
Binary file not shown.
BIN
ft_isnum.o
Normal file
BIN
ft_isnum.o
Normal file
Binary file not shown.
10
ft_p.c
10
ft_p.c
@ -6,7 +6,7 @@
|
|||||||
/* 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/28 18:15:41 by cchauvet ### ########.fr */
|
/* Updated: 2022/11/29 18:13:28 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -28,12 +28,12 @@ void ft_p(int *tab_src, int *tab_dst)
|
|||||||
|
|
||||||
void ft_pb(int *tab_a, int *tab_b)
|
void ft_pb(int *tab_a, int *tab_b)
|
||||||
{
|
{
|
||||||
ft_p(tab_b, tab_a);
|
ft_p(tab_a, tab_b);
|
||||||
ft_putstr("pb");
|
ft_putstr("pb\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_pa(int *tab_a, int *tab_b)
|
void ft_pa(int *tab_a, int *tab_b)
|
||||||
{
|
{
|
||||||
ft_p(tab_a, tab_b);
|
ft_p(tab_b, tab_a);
|
||||||
ft_putstr("pa");
|
ft_putstr("pa\n");
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/08 18:24:45 by cchauvet #+# #+# */
|
/* Created: 2022/11/08 18:24:45 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/11/08 18:26:33 by cchauvet ### ########.fr */
|
/* Updated: 2022/11/29 14:38:32 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
BIN
ft_putstr.o
Normal file
BIN
ft_putstr.o
Normal file
Binary file not shown.
6
ft_r.c
6
ft_r.c
@ -6,7 +6,7 @@
|
|||||||
/* 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/28 17:13:21 by cchauvet ### ########.fr */
|
/* Updated: 2022/11/29 14:39:23 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -26,12 +26,12 @@ static void ft_r(int *tab)
|
|||||||
|
|
||||||
void ft_ra(int *tab)
|
void ft_ra(int *tab)
|
||||||
{
|
{
|
||||||
ft_putstr("ra");
|
|
||||||
ft_r(tab);
|
ft_r(tab);
|
||||||
|
ft_putstr("ra\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_rb(int *tab)
|
void ft_rb(int *tab)
|
||||||
{
|
{
|
||||||
ft_putstr("rb");
|
|
||||||
ft_r(tab);
|
ft_r(tab);
|
||||||
|
ft_putstr("rb\n");
|
||||||
}
|
}
|
||||||
|
24
ft_radix.c
24
ft_radix.c
@ -6,7 +6,7 @@
|
|||||||
/* 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/28 16:29:46 by cchauvet ### ########.fr */
|
/* Updated: 2022/11/30 13:19:13 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,20 +14,22 @@
|
|||||||
|
|
||||||
void ft_radix_sort(int *tab_a, int *tab_b)
|
void ft_radix_sort(int *tab_a, int *tab_b)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
while (ft_bit_finder(ft_get_bit_max(tab_a), 1) != (int) sizeof(int) * 8 - 1)
|
while (!ft_is_sorted(tab_a))
|
||||||
{
|
{
|
||||||
i = 0;
|
i = ft_tablen(tab_a);
|
||||||
while (i < ft_tablen(tab_a))
|
while (--i >= 0 && !ft_is_sorted(tab_a))
|
||||||
{
|
{
|
||||||
if (((tab_a[i] >> (sizeof(int) * 8 - 1)) & 1) == 0)
|
if ((tab_a[i] >> (sizeof(int) * 8 -1) & 1) == 0)
|
||||||
ft_ra(tab_a);
|
ft_pb(tab_a, tab_b);
|
||||||
else
|
else
|
||||||
ft_pa(tab_a, tab_b);
|
ft_ra(tab_a);
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
while (0 < ft_tablen(tab_b))
|
while (tab_b[0] != -1)
|
||||||
ft_pb(tab_b, tab_a);
|
ft_pa(tab_a, tab_b);
|
||||||
|
i = ft_tablen(tab_a);
|
||||||
|
while (--i >= 0 && !ft_is_sorted(tab_a))
|
||||||
|
tab_a[i] = tab_a[i] << 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
BIN
ft_radix.o
Normal file
BIN
ft_radix.o
Normal file
Binary file not shown.
@ -6,7 +6,7 @@
|
|||||||
/* 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/28 18:08:15 by cchauvet ### ########.fr */
|
/* Updated: 2022/11/29 16:19:48 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -25,7 +25,7 @@ static int *ft_values_simplifier(int *tab, int size)
|
|||||||
free(tab);
|
free(tab);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
shift = ft_bit_finder(size, 1);
|
shift = ft_bit_finder_left(size, 1);
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < size)
|
while (i < size)
|
||||||
{
|
{
|
||||||
|
BIN
ft_tablen.o
Normal file
BIN
ft_tablen.o
Normal file
Binary file not shown.
@ -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/28 16:30:39 by cchauvet ### ########.fr */
|
/* Updated: 2022/11/29 16:22:26 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -27,9 +27,12 @@ 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(int *tab);
|
||||||
|
int ft_is_sorted(int *in);
|
||||||
|
|
||||||
int ft_bit_finder(int big, int tofind);
|
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_max(int *tab);
|
||||||
|
int ft_get_bit_min(int *tab);
|
||||||
void ft_radix_sort(int *tab_a, int *tab_b);
|
void ft_radix_sort(int *tab_a, int *tab_b);
|
||||||
|
|
||||||
void ft_swap(int *a, int *b);
|
void ft_swap(int *a, int *b);
|
||||||
|
42
test.c
42
test.c
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/23 16:43:32 by cchauvet #+# #+# */
|
/* Created: 2022/11/23 16:43:32 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/11/28 17:15:24 by cchauvet ### ########.fr */
|
/* Updated: 2022/11/29 17:11:41 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -15,16 +15,38 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int tab[] = {1,2,3,4,-1};
|
int tab_a[5] = {0,1,2,3,-1};
|
||||||
|
int tab_b[5] = {-1};
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = -1;
|
i = 0;
|
||||||
while (tab[++i] != -1)
|
printf("a: ");
|
||||||
printf("%d, ", tab[i]);
|
while (tab_a[i] != -1)
|
||||||
|
{
|
||||||
|
printf("%d, ", tab_a[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
printf("\nb: ");
|
||||||
|
i = 0;
|
||||||
|
while (tab_b[i] != -1)
|
||||||
|
{
|
||||||
|
printf("%d, ", tab_b[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
ft_ra(tab);
|
ft_pa(tab_a, tab_b);
|
||||||
printf("\n");
|
i = 0;
|
||||||
i = -1;
|
printf("a: ");
|
||||||
while (tab[++i] != -1)
|
while (tab_a[i] != -1)
|
||||||
printf("%d, ", tab[i]);
|
{
|
||||||
|
printf("%d, ", tab_a[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
printf("\nb: ");
|
||||||
|
i = 0;
|
||||||
|
while (tab_b[i] != -1)
|
||||||
|
{
|
||||||
|
printf("%d, ", tab_b[i]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user