The Push swap project is a very simple and a highly straightforward algorithm project:
data must be sorted.
🎯 The goal is to sort the stack in an ascending order with the lowest possible number of operations.
make all
./push_swap nb1 nb2 nb3 nbn...
- arguments aren’t integers.
- some arguments are bigger/smaller than an integer.
- there are duplicates.
At the beginning stack b is empty and stack a contains the number given as arguments
Operations | Description |
---|---|
sa | swap first two elements of stack A |
sb | swap first two elements of stack B |
ss | sa and sb at the same time |
pa | pops the first elememt on B and puts it on top of A |
pb | pops the first elememt on A and puts it on top of B |
ra | rotates stuck A up by one |
rb | rotates stuck B up by one |
rr | rotates both A and B up by one |
rra | rotates stuck A down by one |
rrb | rotates stuck B down by one |
rrr | rotates both A and B down by one |
I decided to implement the stack as a double-linked list and I use radix sorting, which works with the binary of the digits.
First I set a rank for each number I have to sort, making the smallest number rank 0, which makes me work only with positive values.
Then I compare each bit and decide whether to move it to stack b or not, repeating this operation until the stack A is sorted.