Compare commits
10 Commits
3fc423de4a
...
master
Author | SHA1 | Date | |
---|---|---|---|
ac54a8f977 | |||
13d20d0ed0 | |||
4a5f0db72a | |||
6a2a2ac866 | |||
32be727085 | |||
ea2251bed4 | |||
224d9bb1a4 | |||
55119130da | |||
afc4f2d0b3 | |||
1d81c47cfd |
6
Makefile
6
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/12/06 13:32:50 by cchauvet ### ########.fr #
|
# Updated: 2022/12/11 19:15:24 by cchauvet ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
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 ft_bitlen.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_bozo_sort.c ft_get_max.c ft_rr.c ft_s.c ft_split.c ft_strlen.c
|
||||||
|
|
||||||
OBJS = ${SRCS:.c=.o}
|
OBJS = ${SRCS:.c=.o}
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ NAME = push_swap
|
|||||||
|
|
||||||
CC = clang
|
CC = clang
|
||||||
|
|
||||||
CFLAGS = -Wall -Werror -Wextra -g
|
CFLAGS = -Wall -Werror -Wextra
|
||||||
|
|
||||||
%.o: %.c
|
%.o: %.c
|
||||||
${CC} ${CFLAGS} -c -o $@ $<
|
${CC} ${CFLAGS} -c -o $@ $<
|
||||||
|
46
README.md
Normal file
46
README.md
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
# Pushswap
|
||||||
|
|
||||||
|
This is my implementation of the Pushswap project for the 42 school, using the radix sort algorithm.
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
Pushswap is a sorting algorithm that sorts a list of integers using two stacks and a set of operations. The goal of the project is to implement the Pushswap algorithm in C and optimize the algorithm to sort the list using the minimum number of operations possible.
|
||||||
|
|
||||||
|
In this implementation, I used the radix sort algorithm, which sorts the list by comparing each digit of the integers in the list, from right to left. The algorithm sorts the integers by "bucketing" them based on their current digit value, and then repeating this process for each subsequent digit.
|
||||||
|
|
||||||
|
The project aims to develop students' problem-solving and algorithmic thinking skills, as well as their ability to optimize code for performance.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
To use the program, first compile the `push_swap` executable by running `make` in the root directory of the project.
|
||||||
|
|
||||||
|
Then, run the program with a list of integers as arguments. For example: `./push_swap 4 67 -10 5`.
|
||||||
|
|
||||||
|
The program will display a list of operations to sort the list, and the number of operations required.
|
||||||
|
|
||||||
|
## Algorithm Overview
|
||||||
|
|
||||||
|
### radix
|
||||||
|
The algorithm consists of the following steps:
|
||||||
|
|
||||||
|
1. replace the value of the number by the index of the number
|
||||||
|
2. put the number on the stack B if the n bit equal 0
|
||||||
|
3. do the same for n + 1 while the stack was not sorted
|
||||||
|
|
||||||
|
## Optimization
|
||||||
|
|
||||||
|
To optimize the algorithm, I used a combination of heuristics and algorithms, such as:
|
||||||
|
|
||||||
|
- Minimizing the number of operations by considering special cases, such as sorted or reverse-sorted lists
|
||||||
|
|
||||||
|
Through experimentation and testing, I was able to significantly reduce the number of operations required to sort the list.
|
||||||
|
|
||||||
|
## Resources
|
||||||
|
|
||||||
|
- [The project subject](./subject.pdf)
|
||||||
|
- The C library documentation for the standard library functions used in the program
|
||||||
|
|
||||||
|
## Authors
|
||||||
|
|
||||||
|
This project was created by Camille CHAUVET. If you have any questions or suggestions, feel free to contact me.
|
||||||
|
|
@ -6,15 +6,15 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/08 18:27:58 by cchauvet #+# #+# */
|
/* Created: 2022/11/08 18:27:58 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/12/02 21:39:08 by cchauvet ### ########.fr */
|
/* Updated: 2022/12/11 17:41:54 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "pushswap.h"
|
#include "pushswap.h"
|
||||||
|
|
||||||
int ft_atoi(char *str)
|
long int ft_atoi(char *str)
|
||||||
{
|
{
|
||||||
int nb;
|
long int nb;
|
||||||
int i;
|
int i;
|
||||||
int sign;
|
int sign;
|
||||||
|
|
||||||
|
60
ft_bozo_sort.c
Normal file
60
ft_bozo_sort.c
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_bozo_sort.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/12/09 14:24:03 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/12/11 15:13:33 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
static void ft_specific_bozo_sort(t_tab tab_a)
|
||||||
|
{
|
||||||
|
if (ft_tablen(tab_a) < 3)
|
||||||
|
{
|
||||||
|
if (!ft_is_sorted(tab_a))
|
||||||
|
ft_ra(tab_a);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (tab_a[0] == tab_a[1] - 1)
|
||||||
|
ft_rra(tab_a);
|
||||||
|
if (tab_a[0] == tab_a[1] + 1 && tab_a[1] == tab_a[2] + 1)
|
||||||
|
{
|
||||||
|
ft_ra(tab_a);
|
||||||
|
ft_sa(tab_a);
|
||||||
|
}
|
||||||
|
if (tab_a[0] == tab_a[1] + 1)
|
||||||
|
ft_sa(tab_a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_bozo_sort(t_tab tab_a, t_tab tab_b)
|
||||||
|
{
|
||||||
|
if (ft_is_sorted(tab_a))
|
||||||
|
return ;
|
||||||
|
while (ft_tablen(tab_a) > 3)
|
||||||
|
{
|
||||||
|
while ((long) tab_a[0] >= ft_tablen(tab_a) + ft_tablen(tab_b) - 3)
|
||||||
|
ft_ra(tab_a);
|
||||||
|
ft_pb(tab_a, tab_b);
|
||||||
|
}
|
||||||
|
ft_specific_bozo_sort(tab_a);
|
||||||
|
if (!ft_is_sorted(tab_a))
|
||||||
|
{
|
||||||
|
ft_pb(tab_a, tab_b);
|
||||||
|
if (!ft_is_sorted(tab_a))
|
||||||
|
ft_ra(tab_a);
|
||||||
|
ft_pa(tab_a, tab_b);
|
||||||
|
if (!ft_is_sorted(tab_a))
|
||||||
|
ft_ra(tab_a);
|
||||||
|
}
|
||||||
|
if (ft_is_sorted(tab_b) && ft_tablen(tab_b) > 1)
|
||||||
|
ft_rb(tab_b);
|
||||||
|
while (ft_tablen(tab_b) != 0)
|
||||||
|
ft_pa(tab_a, tab_b);
|
||||||
|
}
|
@ -6,7 +6,7 @@
|
|||||||
/* 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/12/06 13:30:52 by cchauvet ### ########.fr */
|
/* Updated: 2022/12/09 18:06:59 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,6 +16,10 @@ int ft_is_sorted(unsigned int *tab)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if (ft_tablen(tab) == 0)
|
||||||
|
return (0);
|
||||||
|
if (ft_tablen(tab) == 1)
|
||||||
|
return (1);
|
||||||
i = 1;
|
i = 1;
|
||||||
while (tab[i] != STOP_VALUE)
|
while (tab[i] != STOP_VALUE)
|
||||||
{
|
{
|
||||||
|
18
ft_isnum.c
18
ft_isnum.c
@ -6,7 +6,7 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/08 18:43:06 by cchauvet #+# #+# */
|
/* Created: 2022/11/08 18:43:06 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/11/08 19:52:48 by cchauvet ### ########.fr */
|
/* Updated: 2023/01/19 17:28:17 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,9 +14,23 @@
|
|||||||
|
|
||||||
int ft_isnum(char *str)
|
int ft_isnum(char *str)
|
||||||
{
|
{
|
||||||
|
long int i;
|
||||||
|
int sign;
|
||||||
|
|
||||||
|
sign = 1;
|
||||||
|
if (*str == '-')
|
||||||
|
sign = -1;
|
||||||
|
i = 0;
|
||||||
|
if (*str == '-' || *str == '+')
|
||||||
|
str++;
|
||||||
|
if (*str == '\0')
|
||||||
|
return (0);
|
||||||
while (*str != '\0')
|
while (*str != '\0')
|
||||||
{
|
{
|
||||||
if (!((*str >= '0' && *str <= '9') || *str == '-' || *str == '+'))
|
i = i * 10 + *str - '0';
|
||||||
|
if (i * sign > INT_MAX || INT_MIN > i * sign)
|
||||||
|
return (0);
|
||||||
|
if (!(*str >= '0' && *str <= '9'))
|
||||||
return (0);
|
return (0);
|
||||||
str++;
|
str++;
|
||||||
}
|
}
|
||||||
|
25
ft_p.c
25
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/12/06 17:22:35 by cchauvet ### ########.fr */
|
/* Updated: 2022/12/07 14:15:24 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,16 +14,21 @@
|
|||||||
|
|
||||||
void ft_p(unsigned int *tab_src, unsigned int *tab_dst)
|
void ft_p(unsigned int *tab_src, unsigned int *tab_dst)
|
||||||
{
|
{
|
||||||
int size_src;
|
unsigned int i;
|
||||||
int size_dst;
|
|
||||||
|
|
||||||
size_src = ft_tablen(tab_src);
|
i = ft_tablen(tab_dst) + 1;
|
||||||
size_dst = ft_tablen(tab_dst);
|
while (i > 0)
|
||||||
if (size_src == 0)
|
{
|
||||||
return ;
|
tab_dst[i] = tab_dst[i - 1];
|
||||||
tab_dst[size_dst] = tab_src[size_src - 1];
|
i--;
|
||||||
tab_dst[size_dst + 1] = -1;
|
}
|
||||||
tab_src[size_src - 1] = -1;
|
tab_dst[0] = tab_src[0];
|
||||||
|
i = 0;
|
||||||
|
while (tab_src[i] != STOP_VALUE)
|
||||||
|
{
|
||||||
|
tab_src[i] = tab_src[i + 1];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_pb(unsigned int *tab_a, unsigned int *tab_b)
|
void ft_pb(unsigned int *tab_a, unsigned int *tab_b)
|
||||||
|
@ -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/29 14:38:32 by cchauvet ### ########.fr */
|
/* Updated: 2022/12/09 18:53:38 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
10
ft_r.c
10
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/12/01 17:42:30 by cchauvet ### ########.fr */
|
/* Updated: 2022/12/09 14:36:50 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,7 +19,7 @@ static void ft_r(unsigned int *tab)
|
|||||||
i = 0;
|
i = 0;
|
||||||
while (tab[i + 1] != STOP_VALUE)
|
while (tab[i + 1] != STOP_VALUE)
|
||||||
{
|
{
|
||||||
ft_swap(tab + i, tab + i + 1);
|
ft_swap(tab + i + 1, tab + i);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -29,3 +29,9 @@ void ft_ra(unsigned int *tab)
|
|||||||
ft_r(tab);
|
ft_r(tab);
|
||||||
ft_putstr("ra\n");
|
ft_putstr("ra\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ft_rb(unsigned int *tab)
|
||||||
|
{
|
||||||
|
ft_r(tab);
|
||||||
|
ft_putstr("rb\n");
|
||||||
|
}
|
||||||
|
@ -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/12/06 17:22:52 by cchauvet ### ########.fr */
|
/* Updated: 2022/12/07 14:33:00 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -18,12 +18,12 @@ void ft_radix_sort(unsigned int *tab_a, unsigned int *tab_b)
|
|||||||
unsigned int y;
|
unsigned int y;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (!ft_is_sorted(tab_a) && i < sizeof(unsigned int) * 8)
|
while (!ft_is_sorted(tab_a))
|
||||||
{
|
{
|
||||||
y = ft_tablen(tab_a);
|
y = ft_tablen(tab_a);
|
||||||
while (0 < y && (!ft_is_sorted(tab_a) && !ft_tablen(tab_b)))
|
while (0 < y && !(ft_is_sorted(tab_a) && !ft_tablen(tab_b)))
|
||||||
{
|
{
|
||||||
if (((tab_a[ft_tablen(tab_a) - 1] >> i) & 1) == 1)
|
if (((tab_a[0] >> i) & 1) == 0)
|
||||||
ft_pb(tab_a, tab_b);
|
ft_pb(tab_a, tab_b);
|
||||||
else
|
else
|
||||||
ft_ra(tab_a);
|
ft_ra(tab_a);
|
||||||
|
31
ft_rr.c
Normal file
31
ft_rr.c
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_rr.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/11/28 16:50:03 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/12/09 18:38:14 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
static void ft_rr(unsigned int *tab)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = ft_tablen(tab) - 1;
|
||||||
|
while (i > 0)
|
||||||
|
{
|
||||||
|
ft_swap(tab + i - 1, tab + i);
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_rra(unsigned int *tab)
|
||||||
|
{
|
||||||
|
ft_rr(tab);
|
||||||
|
ft_putstr("rra\n");
|
||||||
|
}
|
24
ft_s.c
Normal file
24
ft_s.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ft_s.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2022/12/09 18:00:12 by cchauvet #+# #+# */
|
||||||
|
/* Updated: 2022/12/09 19:15:57 by cchauvet ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "pushswap.h"
|
||||||
|
|
||||||
|
static void ft_s(t_tab tab)
|
||||||
|
{
|
||||||
|
ft_swap(tab + 0, tab + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_sa(t_tab tab)
|
||||||
|
{
|
||||||
|
ft_s(tab);
|
||||||
|
ft_putstr("sa\n");
|
||||||
|
}
|
@ -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/12/06 17:27:05 by cchauvet ### ########.fr */
|
/* Updated: 2022/12/11 15:25:10 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -33,7 +33,6 @@ static t_tab ft_values_simplifier(long int *tab_in, int size)
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
out[size] = STOP_VALUE;
|
out[size] = STOP_VALUE;
|
||||||
free (tab_in);
|
|
||||||
return (out);
|
return (out);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +60,10 @@ void ft_sort(long int *tab_in, int size_a)
|
|||||||
tab_a = NULL;
|
tab_a = NULL;
|
||||||
if (ft_tab_init(tab_in, size_a, &tab_a, &tab_b) == NULL)
|
if (ft_tab_init(tab_in, size_a, &tab_a, &tab_b) == NULL)
|
||||||
return ;
|
return ;
|
||||||
|
if (ft_tablen(tab_a) > 5)
|
||||||
ft_radix_sort(tab_a, tab_b);
|
ft_radix_sort(tab_a, tab_b);
|
||||||
|
else
|
||||||
|
ft_bozo_sort(tab_a, tab_b);
|
||||||
free(tab_a);
|
free(tab_a);
|
||||||
free(tab_b);
|
free(tab_b);
|
||||||
}
|
}
|
||||||
|
10
ft_split.c
10
ft_split.c
@ -6,13 +6,13 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
|
/* Created: 2022/10/05 19:04:34 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/12/06 17:28:21 by cchauvet ### ########.fr */
|
/* Updated: 2022/12/11 15:10:47 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "pushswap.h"
|
#include "pushswap.h"
|
||||||
|
|
||||||
static void *ft_cancel(char **tab, size_t len)
|
void ft_cancel(char **tab, size_t len)
|
||||||
{
|
{
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
@ -26,7 +26,6 @@ static void *ft_cancel(char **tab, size_t len)
|
|||||||
}
|
}
|
||||||
free(tab);
|
free(tab);
|
||||||
}
|
}
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *ft_substr(char const *s, unsigned int start, size_t len)
|
char *ft_substr(char const *s, unsigned int start, size_t len)
|
||||||
@ -90,7 +89,10 @@ static char **ft_segsplitter(char **tab, size_t len, const char *s, char c)
|
|||||||
let_index++;
|
let_index++;
|
||||||
tab[tab_index] = ft_substr(s, start, let_index - start);
|
tab[tab_index] = ft_substr(s, start, let_index - start);
|
||||||
if (tab[tab_index] == NULL)
|
if (tab[tab_index] == NULL)
|
||||||
return (ft_cancel(tab, tab_index));
|
{
|
||||||
|
ft_cancel(tab, tab_index);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
tab_index++;
|
tab_index++;
|
||||||
}
|
}
|
||||||
return (tab);
|
return (tab);
|
||||||
|
99
main.c
99
main.c
@ -6,32 +6,105 @@
|
|||||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/11/09 17:10:47 by cchauvet #+# #+# */
|
/* Created: 2022/11/09 17:10:47 by cchauvet #+# #+# */
|
||||||
/* Updated: 2022/12/04 19:44:25 by cchauvet ### ########.fr */
|
/* Updated: 2022/12/11 17:52:08 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "pushswap.h"
|
#include "pushswap.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int ft_isin(long int *tab, int len, long int value)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
long int *tab_a;
|
|
||||||
|
|
||||||
tab_a = malloc(argc * sizeof(long int));
|
i = 0;
|
||||||
if (tab_a == NULL)
|
while (i < len)
|
||||||
|
{
|
||||||
|
if (tab[i] == value)
|
||||||
return (1);
|
return (1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_error(char **splitted)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
|
||||||
|
write(2, "Error\n", 6);
|
||||||
|
len = 0;
|
||||||
|
while (splitted[len] != NULL)
|
||||||
|
len++;
|
||||||
|
ft_cancel(splitted, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int ft_parse(long int *tab, const char *s, int index)
|
||||||
|
{
|
||||||
|
char **splitted;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
splitted = ft_split(s, ' ');
|
||||||
|
if (splitted == NULL)
|
||||||
|
return (1);
|
||||||
|
i = -1;
|
||||||
|
if (splitted[0] == NULL)
|
||||||
|
write(2, "Error\n", 6);
|
||||||
|
while (splitted[++i] != NULL)
|
||||||
|
{
|
||||||
|
if (!ft_isnum(splitted[i]))
|
||||||
|
{
|
||||||
|
ft_error(splitted);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
tab[index + i] = ft_atoi(splitted[i]);
|
||||||
|
if (ft_isin(tab, index + i, tab[index + i]))
|
||||||
|
{
|
||||||
|
ft_error(splitted);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ft_cancel(splitted, i + 1);
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ft_total_len(int argc, char **argv)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
i = 1;
|
i = 1;
|
||||||
while (i < argc)
|
while (i < argc)
|
||||||
{
|
{
|
||||||
if (!ft_isnum(argv[i]))
|
len += ft_seglen(argv[i], ' ');
|
||||||
{
|
|
||||||
write(2, "Error\n", 6);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
tab_a[i - 1] = ft_atoi(argv[i]);
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
if (argc > 2)
|
return (len);
|
||||||
ft_sort(tab_a, i - 1);
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int y;
|
||||||
|
size_t len;
|
||||||
|
long int *tab_a;
|
||||||
|
|
||||||
|
tab_a = malloc(ft_total_len(argc, argv) * sizeof(long int));
|
||||||
|
if (tab_a == NULL)
|
||||||
|
return (1);
|
||||||
|
i = 0;
|
||||||
|
len = 0;
|
||||||
|
while (++i < argc)
|
||||||
|
{
|
||||||
|
y = ft_parse(tab_a, argv[i], (int) len);
|
||||||
|
if (y == 0)
|
||||||
|
{
|
||||||
|
free(tab_a);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
len += y;
|
||||||
|
}
|
||||||
|
if (len >= 2)
|
||||||
|
ft_sort(tab_a, len);
|
||||||
|
free(tab_a);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
Submodule push_swap_tester deleted from 59628457df
11
pushswap.h
11
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/12/06 17:30:20 by cchauvet ### ########.fr */
|
/* Updated: 2022/12/11 17:55:38 by cchauvet ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,9 +19,11 @@
|
|||||||
|
|
||||||
int ft_isnum(char *str);
|
int ft_isnum(char *str);
|
||||||
size_t ft_strlen(const char *s);
|
size_t ft_strlen(const char *s);
|
||||||
int ft_atoi(char *str);
|
long int ft_atoi(char *str);
|
||||||
void ft_putstr(char *str);
|
void ft_putstr(char *str);
|
||||||
|
size_t ft_seglen(const char *s, char c);
|
||||||
char **ft_split(const char *s, char c);
|
char **ft_split(const char *s, char c);
|
||||||
|
void ft_cancel(char **str, size_t len);
|
||||||
|
|
||||||
void ft_sort(long int *tab, int size);
|
void ft_sort(long int *tab, int size);
|
||||||
|
|
||||||
@ -33,12 +35,15 @@ int ft_is_sorted(unsigned int *in);
|
|||||||
unsigned int ft_bitlen(unsigned int nb);
|
unsigned int ft_bitlen(unsigned int nb);
|
||||||
|
|
||||||
void ft_radix_sort(unsigned int *tab_a, unsigned int *tab_b);
|
void ft_radix_sort(unsigned int *tab_a, unsigned int *tab_b);
|
||||||
|
void ft_bozo_sort(unsigned int *tab_a, unsigned int *tab_b);
|
||||||
void ft_swap(unsigned int *a, unsigned int *b);
|
void ft_swap(unsigned int *a, unsigned int *b);
|
||||||
void ft_pa(unsigned int *tab_a, unsigned int *tab_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_pb(unsigned int *tab_a, unsigned int *tab_b);
|
||||||
void ft_ra(unsigned int *tab_a);
|
void ft_ra(unsigned int *tab_a);
|
||||||
void ft_rb(unsigned int *tab_b);
|
void ft_rb(unsigned int *tab_b);
|
||||||
|
void ft_rra(unsigned int *tab_a);
|
||||||
|
void ft_rrb(unsigned int *tab_b);
|
||||||
|
void ft_sa(unsigned int *tab_a);
|
||||||
|
|
||||||
typedef unsigned int *t_tab;
|
typedef unsigned int *t_tab;
|
||||||
#endif
|
#endif
|
||||||
|
BIN
subject.pdf
Normal file
BIN
subject.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user