Push-Swap Simulator - Help
What is Push-Swap?
Push-swap is a sorting challenge where you must sort a stack of integers (stack A) using only two stacks and a limited set of operations.
The goal is to achieve a sorted sequence in stack A with stack B empty, using the minimum number of operations.
The Operations
The push-swap problem operates on two LIFO stacks (A and B) with 11 permitted operations:
| Operation |
Description |
sa | Swap top two elements of stack A |
sb | Swap top two elements of stack B |
ss | Swap top two elements of both stacks simultaneously |
pa | Push top element from B to A |
pb | Push top element from A to B |
ra | Rotate stack A (top element goes to bottom) |
rb | Rotate stack B (top element goes to bottom) |
rr | Rotate both stacks simultaneously |
rra | Reverse rotate stack A (bottom element goes to top) |
rrb | Reverse rotate stack B (bottom element goes to top) |
rrr | Reverse rotate both stacks simultaneously |
Initial state: Unsorted integers in stack A, empty stack B
Goal state: Sorted integers in stack A (smallest at top), empty stack B
Key Features
Single Simulation Mode
Interactive Visualization
Color-coded elements showing relative values. The size of bars is also related to their values - larger numbers have wider bars.
When the stack is correctly sorted, the stacks are highlighted in green
Infinite Scroll
Navigate easily through large stacks to see top and bottom elements simultaneously.
Use your mouse wheel or drag an element to scroll through the stack elements
Highlight elements
Right-click on stack visualization to input number sequences and highlight them in the stack
Move Editing
Edit move sequences with three modes: truncate, insert, and overwrite
Manual Control
Execute operations manually via button controls or automatic playback (with adjustable speed)
Navigation
Step forward/backward through moves with adjustable animation speed
Clipboard Support
Copy/paste move sequences for easy sharing and testing
State Persistence
Automatic session recovery via localStorage
Comparison Mode
Dual Simulators
Run two solutions side-by-side with independent move sequences
Convergence Detection
Identify where solutions reach identical states
Difference Highlighting
Color-coded divergent segments:
• Orange: Different but same length
• Green: This solution is better
• Red: Other solution is better
Move Merging
Combine optimal segments from both solutions to create better results
How to Use
🎯 Single Solution Testing
- Enter numbers in the top left input field. Alternatively, enter the number of elements and click "Random" to generate random data
- Click "Apply/Reset Stacks" to initialize the stacks
- Execute moves manually using the operation buttons or paste a sequence
- Use playback controls to review the visualization
💡 Pro Tips:
- Scroll stacks to see both head and tail values
- If you search where is a specific integer, right click on a stack and enter the searched integer(s). This will highlight it(them)
⚖️ Solution Comparison/Optimization
- Enter or generate numbers and apply to both simulators
- Enable "Compare: OFF" to "Compare: ON"
- Paste sequence A in the left simulator
- Paste sequence B in the right simulator
- Use "FIND NEXT DIFF" to locate differences
- Review highlighted segments to analyze performance
- You may also use the merge buttons (1→2 or 1←2) to copy the better segment
- Continue finding and merging differences
- Export your optimized solution
Interface Elements
Header Controls
- Number series input: Enter custom integers to sort
- Random size: Enter size of random integers to generate
- Random button: Generate random integers
- Apply/Reset Stacks: Initialize or reset both stacks
- Compare toggle: Switch between single and dual-simulator mode
Sync Controls (Compare Mode)
- PREV/NEXT: Step backward/forward through both sequences
- FAST FWD/STOP: Play both sequences simultaneously (The speed can be adjusted with the speed slider of version 1)
- FIND NEXT DIFF: Jump to the next difference between solutions
- FIND CONVERGENCE: Find the point where both solutions converge
- SKIP DIFF: Skip the current difference segment
- Merge buttons: Copy segments between solutions
Stack Visualization
- Color coding: Elements are colored based on their relative values
- Right-click: Highlight specific number sequences in the stack by entering them in a dialog
- Infinite scroll: Use your mouse wheel or drag an element to scroll through the stack elements
Move List
- Edit modes: Truncate, Insert, or Overwrite existing moves
- Click navigation: Click on any move to jump to that position
- Clipboard: Copy/paste move sequences using the clipboard buttons
Development
This simulator is a pure JavaScript web application built with modern web technologies. It runs entirely in the browser and requires no server-side components.
For development setup or assistance, please visit the GitHub repository.
📝 Note: This help page can be kept open while you work with the simulator for quick reference. Use Alt+Tab to switch between windows.