push_swap is a C++ program designed to sort a stack of unique integers using a limited set of operations. The goal is to achieve sorting with the minimum number of operations possible. 🎯
- Two stacks are used:
aandb. - Initially, stack
acontains a random set of unique integers, while stackbis empty. - The sorting must be done in
ausing the following allowed operations:
sa: Swap the top two elements of stacka.sb: Swap the top two elements of stackb.ss: Swap the top two elements of both stacksaandbsimultaneously.
pa: Move the top element ofbto the top ofa.pb: Move the top element ofato the top ofb.
ra: Shift all elements ofaup by one position.rb: Shift all elements ofbup by one position.rr: Shift all elements of bothaandbup by one position.
rra: Shift all elements ofadown by one position.rrb: Shift all elements ofbdown by one position.rrr: Shift all elements of bothaandbdown by one position.
Stack A: 2 1 3 6 5 8 Stack B: _ _
sa # Swap 2 and 1
pb
pb
pb # Push top three elements from A to B
ra
rb # Rotate both stacks
rra
rrb # Reverse rotate both stacks
sa # Swap again
pa
pa
pa # Push elements back to A
- Program Name:
push_swap - Files:
Makefile,*.h,*.c - Allowed Functions:
read,write,malloc,free,exit - Libraries:
libftis allowed - Constraints:
- ❌ No global variables
- 🔢 Must output the smallest number of instructions to sort
a - 📝 Output must be newline-separated
- 🚨 Must return an error for invalid inputs
- $ ./push_swap 0 one 2 3
- Error
- To check your sorting efficiency:
- $ ARG="4 67 3 87 23"; ./push_swap $ARG | wc -l
- This project uses Radix Sort, an efficient non-comparative sorting algorithm, optimized for the given constraints.
- push_swap is a challenging sorting algorithm project that requires optimizing operations to sort a stack with the fewest possible moves. Efficient implementation and algorithmic strategy are key to achieving a competitive solution. 🚀