The Push Swap project is an algorithm challenge aimed at sorting a stack of integers in the least number of moves, using a constrained set of operations. This project serves as a way to deepen understanding of sorting algorithms and optimize problem-solving techniques.
This repository is dedicated to the implementation of an efficient solution to sort the stack using the minimum number of operations. The project does not include a checker, so the validation must be done with external tools or the checker provided by 42.
The Push Swap project is organized into different components based on the operations allowed to manipulate two stacks (A and B). The mandatory part includes creating algorithms to sort small to large sequences using the following operations:
sa
,sb
,ss
: Swap operations on stack A, stack B, or both.pa
,pb
: Push operations to move the top element from one stack to another.ra
,rb
,rr
: Rotate operations to move the top element to the bottom.rra
,rrb
,rrr
: Reverse rotate operations to move the bottom element to the top.
The project is focused on optimization and efficiency, aiming to minimize the number of moves for different stack sizes.
For detailed documentation, including usage examples and function breakdowns, please visit the link below:
To clone this repository and compile the project, run the following commands:
git clone https://github.com/pin3dev/42_Push_Swap.git
cd 42_Push_Swap/pushswap
This will download the project from GitHub into your local machine. Once inside the 42_Push_Swap
directory, you can compile the project using the provided Makefile.
A Makefile
is provided to simplify the compilation process. The Makefile includes the following rules:
all
: Compiles the project.clean
: Removes object files.fclean
: Removes object files and the executable.re
: Recompiles the entire project from scratch.
To compile the project, simply run:
make
This will generate the push_swap
executable, which can be used to sort a list of integers.
To run the Push Swap algorithm on a list of integers:
-
Execute the program with a set of integers as arguments:
./push_swap 3 5 2 1 4
-
The program will output a sequence of operations needed to sort the integers:
pb pb sa pa rra pa rra rra
You can also test the number of operations by piping the output to wc -l
:
./push_swap 3 5 2 1 4 | wc -l
This project strictly follows the 42 School Norm coding guidelines, which significantly influenced certain decisions in its implementation. These rules may sometimes lead to seemingly inefficient or unusual solutions, but they were necessary to meet the strict requirements of the school.
All the theoretical material used to develop this project is organized and can be accessed directly via the link below:
A step-by-step tutorial is available and can be followed to complete the project. It is linked in the button below.