Push_Swap
is a coding project from the 42 school curriculum, designed to introduce students to complex algorithms, particularly focusing on sorting algorithms and stack operations. The objective is to sort data on a stack with a limited set of operations, using the smallest number of actions. The project is divided into two programs:
push_swap
: This program calculates and displays a list of operations that sort a stack of integers.checker
: This is a supplementary program that checks if the list of operations from thepush_swap
program actually sorts the stack.
- Efficient Sorting Algorithm: Implements an advanced sorting algorithm to sort numbers in ascending order with minimal stack operations.
- Error Handling: Robust validation of input data to handle errors gracefully.
- Visualization Tool: Includes a visualizer to watch the sorting algorithm in action, enhancing understanding and debugging.
The sorting operations allowed in this project are limited to manipulating the two stacks, A and B, as follows:
sa
(swap A): Swap the first two elements at the top of stack A.sb
(swap B): Swap the first two elements at the top of stack B.ss
:sa
andsb
at the same time.pa
(push A): Take the first element at the top of B and put it at the top of A.pb
(push B): Take the first element at the top of A and put it at the top of B.ra
(rotate A): Shift up all elements of stack A by 1.rb
(rotate B): Shift up all elements of stack B by 1.rr
:ra
andrb
at the same time.rra
(reverse rotate A): Shift down all elements of stack A by 1.rrb
(reverse rotate B): Shift down all elements of stack B by 1.rrr
:rra
andrrb
at the same time.
- GCC compiler
- Make
- Ruby (optional, for generating random numbers)
Clone the repository and compile the programs using the Makefile:
git clone https://yourrepositorylink.com/push_swap.git
cd push_swap
make
Running push_swap To run push_swap, you need to provide a series of space-separated integers:
./push_swap 42 21 17 6 8
This will output a series of operations that sort the numbers.
To verify that the operations output by push_swap are correct, you can pipe its output to checker:
ARG="42 21 17 6 8"; ./push_swap $ARG | ./checker $ARG
The bonus part includes the checker program, which can be compiled using:
make bonus
The visualizer can help you see the sorting process. To use it, download and build the visualizer with the following command:
make v
Then, run the visualizer (only works in bash):
ARG=$(ruby -e "puts (0..500).to_a.shuffle.join(' ')"); ./push_swap ${ARG} | ./pro_checker ${ARG}
Visualizer used is: https://github.com/ailopez-o/42Barcelona-PushSwap-ProChecker