Compare commits
6 Commits
224d9bb1a4
...
master
Author | SHA1 | Date | |
---|---|---|---|
ac54a8f977 | |||
13d20d0ed0 | |||
4a5f0db72a | |||
6a2a2ac866 | |||
32be727085 | |||
ea2251bed4 |
6
Makefile
6
Makefile
@ -6,11 +6,11 @@
|
||||
# By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/11/28 16:34:37 by cchauvet #+# #+# #
|
||||
# Updated: 2022/12/09 18:03:57 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_bozo_sort.c ft_get_max.c ft_rr.c ft_s.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}
|
||||
|
||||
@ -18,7 +18,7 @@ NAME = push_swap
|
||||
|
||||
CC = clang
|
||||
|
||||
CFLAGS = -Wall -Werror -Wextra -g
|
||||
CFLAGS = -Wall -Werror -Wextra
|
||||
|
||||
%.o: %.c
|
||||
${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 +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
int ft_atoi(char *str)
|
||||
long int ft_atoi(char *str)
|
||||
{
|
||||
int nb;
|
||||
long int nb;
|
||||
int i;
|
||||
int sign;
|
||||
|
||||
|
@ -6,14 +6,21 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/12/09 14:24:03 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/12/09 19:14:50 by cchauvet ### ########.fr */
|
||||
/* Updated: 2022/12/11 15:13:33 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "pushswap.h"
|
||||
|
||||
static void ft_specific_bozo_sort(t_tab tab_a, t_tab tab_b)
|
||||
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)
|
||||
@ -23,6 +30,7 @@ static void ft_specific_bozo_sort(t_tab tab_a, t_tab tab_b)
|
||||
}
|
||||
if (tab_a[0] == tab_a[1] + 1)
|
||||
ft_sa(tab_a);
|
||||
}
|
||||
}
|
||||
|
||||
void ft_bozo_sort(t_tab tab_a, t_tab tab_b)
|
||||
@ -35,6 +43,7 @@ void ft_bozo_sort(t_tab tab_a, t_tab tab_b)
|
||||
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);
|
||||
|
BIN
ft_bozo_sort.o
BIN
ft_bozo_sort.o
Binary file not shown.
BIN
ft_get_max.o
BIN
ft_get_max.o
Binary file not shown.
BIN
ft_is_sorted.o
BIN
ft_is_sorted.o
Binary file not shown.
18
ft_isnum.c
18
ft_isnum.c
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
{
|
||||
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')
|
||||
{
|
||||
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);
|
||||
str++;
|
||||
}
|
||||
|
BIN
ft_isnum.o
BIN
ft_isnum.o
Binary file not shown.
BIN
ft_putstr.o
BIN
ft_putstr.o
Binary file not shown.
BIN
ft_radix.o
BIN
ft_radix.o
Binary file not shown.
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/11/08 19:47:56 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/12/09 14:40:33 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++;
|
||||
}
|
||||
out[size] = STOP_VALUE;
|
||||
free (tab_in);
|
||||
return (out);
|
||||
}
|
||||
|
||||
|
10
ft_split.c
10
ft_split.c
@ -6,13 +6,13 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
static void *ft_cancel(char **tab, size_t len)
|
||||
void ft_cancel(char **tab, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
@ -26,7 +26,6 @@ static void *ft_cancel(char **tab, size_t len)
|
||||
}
|
||||
free(tab);
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
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++;
|
||||
tab[tab_index] = ft_substr(s, start, let_index - start);
|
||||
if (tab[tab_index] == NULL)
|
||||
return (ft_cancel(tab, tab_index));
|
||||
{
|
||||
ft_cancel(tab, tab_index);
|
||||
return (NULL);
|
||||
}
|
||||
tab_index++;
|
||||
}
|
||||
return (tab);
|
||||
|
BIN
ft_tablen.o
BIN
ft_tablen.o
Binary file not shown.
94
main.c
94
main.c
@ -6,33 +6,105 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/11/09 17:10:47 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/12/09 15:00:05 by cchauvet ### ########.fr */
|
||||
/* Updated: 2022/12/11 17:52:08 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "pushswap.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
int ft_isin(long int *tab, int len, long int value)
|
||||
{
|
||||
int i;
|
||||
long int *tab_a;
|
||||
|
||||
tab_a = malloc(argc * sizeof(long int));
|
||||
if (tab_a == NULL)
|
||||
i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
if (tab[i] == value)
|
||||
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;
|
||||
while (i < argc)
|
||||
{
|
||||
if (!ft_isnum(argv[i]))
|
||||
len += ft_seglen(argv[i], ' ');
|
||||
i++;
|
||||
}
|
||||
return (len);
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
write(2, "Error\n", 6);
|
||||
free(tab_a);
|
||||
return (1);
|
||||
}
|
||||
tab_a[i - 1] = ft_atoi(argv[i]);
|
||||
i++;
|
||||
len += y;
|
||||
}
|
||||
if (argc > 2)
|
||||
ft_sort(tab_a, i - 1);
|
||||
if (len >= 2)
|
||||
ft_sort(tab_a, len);
|
||||
free(tab_a);
|
||||
return (0);
|
||||
}
|
||||
|
Submodule push_swap_tester deleted from 59628457df
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/11/08 18:19:44 by cchauvet #+# #+# */
|
||||
/* Updated: 2022/12/09 18:51:58 by cchauvet ### ########.fr */
|
||||
/* Updated: 2022/12/11 17:55:38 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,14 +15,15 @@
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <limits.h>
|
||||
//# include <stdio.h>
|
||||
# define STOP_VALUE 4294967295
|
||||
|
||||
int ft_isnum(char *str);
|
||||
size_t ft_strlen(const char *s);
|
||||
int ft_atoi(char *str);
|
||||
long int ft_atoi(char *str);
|
||||
void ft_putstr(char *str);
|
||||
size_t ft_seglen(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);
|
||||
|
||||
@ -44,5 +45,5 @@ 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
|
||||
|
BIN
subject.pdf
Normal file
BIN
subject.pdf
Normal file
Binary file not shown.
Reference in New Issue
Block a user