Push-Swap Simulator Icon

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
saSwap top two elements of stack A
sbSwap top two elements of stack B
ssSwap top two elements of both stacks simultaneously
paPush top element from B to A
pbPush top element from A to B
raRotate stack A (top element goes to bottom)
rbRotate stack B (top element goes to bottom)
rrRotate both stacks simultaneously
rraReverse rotate stack A (bottom element goes to top)
rrbReverse rotate stack B (bottom element goes to top)
rrrReverse 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

  1. Enter numbers in the top left input field. Alternatively, enter the number of elements and click "Random" to generate random data
  2. Click "Apply/Reset Stacks" to initialize the stacks
  3. Execute moves manually using the operation buttons or paste a sequence
  4. 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

  1. Enter or generate numbers and apply to both simulators
  2. Enable "Compare: OFF" to "Compare: ON"
  3. Paste sequence A in the left simulator
  4. Paste sequence B in the right simulator
  5. Use "FIND NEXT DIFF" to locate differences
  6. Review highlighted segments to analyze performance
  7. You may also use the merge buttons (1→2 or 1←2) to copy the better segment
  8. Continue finding and merging differences
  9. Export your optimized solution

Interface Elements

Header Controls

Sync Controls (Compare Mode)

Stack Visualization

Move List

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.