Because Swap_push isn’t as natural
The Push_swap project is a very simple and highly effective algorithm project: data will need to be sorted. You have at your disposal a set of int values, 2 stacks and a set of instructions to manipulate both stacks. Your goal? Write a program in C called push_swap which calculates and displays on the standard output the smallest program using Push_swap instruction language that sorts the integer arguments received. Easy? We’ll see about that...
To use it:
git clone https://github.com/benmaia/42_Push_Swap.git
To run, use make to compile it and create the ./push_swap, and pass the arguments as intengers
make && ./push_swap 0 -100 90 1042 -9 50 6 -2
Make sure you don't repeat numbers, only use intengers (don't overflow, underflow, or use floats and doubles), put at least 1 argument and that only contains digits, or it will display an Error message!
To sort the numbers, we only had 2 stacks (a and b) and 4 types of movements, swap, push, rotate, and reverse rotate:
Swap
as the name says swaps the position of the 2 numbers on top of the stack, you can sa (swap the a stack), sb (swap the b stack) and ss (swap both stacks at the same time).Push
as the name says pop the number on top of one stack and pushes it to the top of the other stack, you can pa (pop from b, and push to a), pb (pop from a and pushes it to b).Rotate
changes the top number of the stack to the bottom, and every number shifts 1 position up , you can ra (rotate the a stack), rb (rotate the b stack) and rr (rotate both stacks at the same time).Reverse rotate
, does the oposite of the rotate, changes the bottom number of the stack to the top, and every number shifts 1 position down , you can rra (reverse rotate the a stack), rrb (reverse rotate the b stack) and rrr (reverse rotate both stacks at the same time).
The output will be the the right movements to order the numbers
My algorithm takes 1084
movements to sort 100 numbers (it's a 3/5), and takes 6784
movements to sort 500 numbers (it's a 4/5).
The learning objectives of this project are rigor, use of C and use of basic algorithms. Especially looking at the complexity of these basic algorithms. Sorting values is simple. To sort them the fastest way possible is less simple, especially because from one integers configuration to another, the most efficient sorting algorithmcan differ.
Push_Swap OK ✅
Grade: 84%