A stack-based sorting project using a quicksort-inspired algorithm.
push_swap is a 42 School project that sorts a stack of integers using a limited set of operations and two stacks: A
and B
. The goal is to sort stack A
with the fewest possible instructions using only push, swap, rotate, and reverse operations.
This implementation is powered by the quicksort algorithm, adapted to work within the push_swap constraints. It emphasizes algorithmic efficiency, memory control, and operation optimization.
sa
,sb
,ss
β swap the top elementspa
,pb
β push from one stack to the otherra
,rb
,rr
β rotate stack uprra
,rrb
,rrr
β reverse rotate stack down
The algorithm works by recursively dividing the stack into chunks, selecting pivots, and using quicksort-style partitioning while relying on the allowed instructions to move elements between stacks.
./push_swap 4 2 5 3 1
# Output:
pb
pb
sa
pa
pa
ra
ra