Fifth project for 42 school. The aim of this project is to sort data on a stack, with a limited set of instructions, using the lowest possible number of actions.
We have 2 stacks (named A and B) and a limited set of instructions. At the beginning, stack A contains a random amount of negative and/or positive numbers (without duplicate) and stack B is empty. The goal is to sort in ascending order numbers into stack A.
The available instructions are as follow:
Name | Description |
---|---|
sa |
Swap the first 2 elements at the top of stack a. |
sb |
Swap the first 2 elements at the top of stack b. |
ss |
sa and sb at the same time. |
pa |
Take the first element at the top of b and put it at the top of a. |
pb |
Take the first element at the top of a and put it at the top of b. |
ra |
Shift up all elements of stack a by 1. |
rb |
Shift up all elements of stack b by 1. |
rr |
ra and rb at the salme time. |
rra |
Shift down all elements of stack a by 1. |
rrb |
Shift down all elements of stack b by 1. |
rrr |
rra and rrb at the same time. |
make #to compile the program
./push_swap [INT LIST]
./push_swap 3 5 4 1 2
Returns
ra
pb
pb
pa
ra
pa
ra
Medium article - Push_Swap: The least amount of moves with two stacks
push_swap_tester by @laisarena
push_swap_tester by @LeoFu9487
push_swap visualizer by @o-reo