Push Swap is a project that consists of sorting a stack of integers with a limited set of operations using two stacks (a
and b
). The goal is to achieve an efficient sorting algorithm with the least number of operations.
- Develop an efficient sorting algorithm for a stack of numbers.
- Implement the sorting using only a predefined set of operations.
- Optimize the number of operations to achieve the best possible performance.
- Handle edge cases such as duplicates, negative numbers, and large inputs.
sa
(swap a): Swap the first two elements of stacka
.sb
(swap b): Swap the first two elements of stackb
.ss
(sa + sb): Swap both stacks simultaneously.
pa
(push a): Move the top element ofb
toa
.pb
(push b): Move the top element ofa
tob
.
ra
(rotate a): Shift all elements ofa
up by one.rb
(rotate b): Shift all elements ofb
up by one.rr
(ra + rb): Rotate both stacks simultaneously.
rra
(reverse rotate a): Shift all elements ofa
down by one.rrb
(reverse rotate b): Shift all elements ofb
down by one.rrr
(rra + rrb): Reverse rotate both stacks simultaneously.
To compile the project, run:
make
This will generate the push_swap
executable.
To run the program, use:
./push_swap <list_of_numbers>
./push_swap 3 2 5 1 4
This will output a sequence of operations to sort the given list.
- The program must handle invalid inputs (non-numeric characters, duplicates, etc.).
- If an error occurs, the program should print
Error
and exit with a status of1
.
- Small sets (≤5 numbers): Implement a simple sorting strategy using minimal operations.
- Larger sets: Use an optimized algorithm like:
- Chunk sorting: Dividing the stack into smaller chunks.
- QuickSort-based approach: Sorting based on pivot selection.
- Radix Sort: Efficient sorting for large numbers.
The bonus part includes:
- Implementing a checker program (
checker
) to verify if a sequence of operations correctly sorts a stack. - Handling more complex sorting scenarios efficiently.