Push_swap is a sorting algorithm project that aims to sort a stack of integers using a limited set of operations while minimizing the number of moves. The goal is to implement two programs: one that checks if a given list of instructions sorts the stack, and another that generates the list of instructions to sort the stack.
Follow the steps below to test the project:
- Clone the repository:
$> git clone https://github.com/Sepahsalar/42-push_swap.git
- Compile the project:
$> make
- Run the program with the following syntax:
$> ./push_swap num1 num2 num3 num4
- num: unique numbers in a non-ascending order.
$> ./push_swap 8 5 2 4 9
Sort a random list of integers using the smallest number of moves, two stacks and a limited set of operations. You start with two empty stacks: a and b. You are given a random list of integers via command line arguments.
Only these moves are allowed:
sa
: swapa
- Swap the first2
elements at the top of stacka
. Do nothing if there is only one or no elements.sb
: swapb
- Swap the first2
elements at the top of stackb
. Do nothing if there is only one or no elements.ss
:sa
andsb
at the same time.pa
: pusha
- Take the first element at the top ofb
and put it at the top ofa
. Do nothing ifb
is empty.pb
: pushb
- Take the first element at the top ofa
and put it at the top ofb
. Do nothing ifa
is empty.ra
: rotate a - Shift up all elements of stacka
by1
. The first element becomes the last one.rb
: rotate b - Shift up all elements of stackb
by1
. The first element becomes the last one.rr
:ra
andrb
at the same time.rra
: reverse rotatea
- Shift down all elements of stack a by1
. The last element becomes the first one.rrb
: reverse rotateb
- Shift down all elements of stack b by1
. The last element becomes the first one.rrr
:rra
andrrb
at the same time.
At the end, stack b
must empty empty and all integers must be in stack a
, sorted in ascending order.
-
To initiate the sorting process, my code first pushes the top two elements from stack
a
to stackb
. This creates the smallest and largest numbers in stackb
, which are prerequisites for my code. Before pushing a number from stacka
to stackb
, the algorithm compares the number being pushed with the smallest and largest numbers in stackb
. -
At this stage, the algorithm examines each number in stack
a
to determine which requires the minimum number of operations to be placed correctly in stackb
. -
Following this, the algorithm determines which number to push, calculates the required rotations for both stacks, and performs rotations accordingly. If one stack requires more rotations, the algorithm completes the remaining rotations in that stack.
-
Subsequently, the algorithm pushes the number from the top of stack
a
to the top of stackb
. The spot in stackb
is ensured to be the correct position for the number due to previous calculations. This pushing process continues until only three elements remain in stacka
. -
The algorithm efficiently sorts the remaining three members in stack
a
. -
Each member in stack
b
is then pushed one by one to stacka
from top to bottom. However, the algorithm verifies each element before pushing it. This process continues until stackb
is empty. -
Finally, the algorithm applies the required amount of rotation one last time to bring the smallest number to the top of stack
a
.