Push Swap is a project at 42 School that challenges you to implement a sorting algorithm using two stacks. The goal is to sort a set of integers with a limited set of operations while minimizing the number of moves.
- sa : swap a - Swap the first 2 elements at the top of stack A. Do nothing if there is only one or no elements.
- sb : swap b - Swap the first 2 elements at the top of stack B. Do nothing if there is only one or no elements.
- ss : sa and sb at the same time.
- pa : push a - take the first element at the top of b and put it at the top of a. Do nothing if B is empty.
- pb : push b - take the first element at the top of a and put it at the top of b. Do nothing if A is empty.
- ra : rotate a - shift up all elements of stack A by 1. The first element becomes the last one.
- rb : rotate b - shift up all elements of stack B by 1. The first element becomes the last one.
- rr : ra and rb at the same time.
- rra : reverse rotate a - shift down all elements of stack A by 1. The last element becomes the first one.
- rrb : reverse rotate b - shift down all elements of stack B by 1. The last element becomes the first one.
- rrr : rra and rrb at the same time.
-
Clone the repository and enter the project folder:
git clone https://github.com/siemeris/push_swap.git cd push_swap
-
Compile the project:
make
Run the program by providing a list of integers as arguments:
./push_swap 8 4 -217 2 -2147483648 0
Running the ./checker:
If you have problems running the
./checker
, usechmod 777 ./checker
and try again.
./push_swap [numbers] | ./checker [numbers]
./push_swap 9 0 -217 2147483647 -2147483648 | ./checker 9 0 -217 2147483647 -2147483648
The project structure is organized as follows:
push_swap/
: Contains the project source code../libft/
: 42 standard library, first project performed in the cursus../Makefile
: File for compiling the project.
-
Sorting a list of integers:
./push_swap 9 4 2 7 1
-
Checking if the list is sorted:
./push_swap 3 1 5 | ./checker 3 1 5
If you wish to contribute to this project, please create an issue or submit a pull request.