A modern and blazingly fast stack sorting algorithm written in Go, featuring a stunning web-based visualizer
GoSortStack is a sorting algorithm that sorts a random list of integers using the minimum number of moves possible. It operates with two stacks (A and B) and a limited set of operations.
| Operation | Description |
|---|---|
SwapA |
Swap the first 2 elements at the top of stack A |
SwapB |
Swap the first 2 elements at the top of stack B |
SwapBoth |
SwapA and SwapB at the same time |
PushA |
Take the first element at the top of B and put it at the top of A |
PushB |
Take the first element at the top of A and put it at the top of B |
RotateA |
Shift up all elements of stack A by 1 |
RotateB |
Shift up all elements of stack B by 1 |
RotateBoth |
RotateA and RotateB at the same time |
ReverseRotateA |
Shift down all elements of stack A by 1 |
ReverseRotateB |
Shift down all elements of stack B by 1 |
ReverseRotateBoth |
ReverseRotateA and ReverseRotateB at the same time |
Goal: Stack B must be empty, and all integers must be in stack A, sorted in ascending order.
# Clone the repository
git clone https://github.com/kwa0x2/GoSortStack.git
cd GoSortStack
# Build the project
go build -o sort_stack.exe cmd/main.go./sort_stack.exe 5 2 8 1 9 3 7./sort_stack.exe -v 5 2 8 1 9 3 7or
./sort_stack.exe --visualize 5 2 8 1 9 3 7This implementation uses the Turk Algorithm, a cost-based optimal sorting strategy that minimizes the number of operations.
Push the first 2 elements from Stack A to Stack B
- This creates a minimum and maximum value in Stack B
- Provides reference points for subsequent elements
For each element in Stack A, calculate the minimum number of operations required to place it in the correct position in Stack B
- Compare 4 different rotation strategies:
RotateA + RotateB(Rotate both stacks upward)ReverseRotateA + ReverseRotateB(Rotate both stacks downward)RotateA + ReverseRotateB(A up, B down)ReverseRotateA + RotateB(A down, B up)
Push the element with the lowest cost to Stack B
- This process continues until only 3 elements remain in Stack A
Quickly sort the remaining 3 elements in Stack A
Push all elements from Stack B back to Stack A in optimal positions
- Each element is placed in its correct sorted position
Rotate Stack A to bring the smallest element to the top
- โ Efficient: Minimizes the number of operations
- โ Optimal: Cost-based approach selects the best rotation strategy
- โ Scalable: Works with 100, 500, even 1000+ elements
- โ Fast: Leverages Go's performance for lightning-fast execution
For a detailed explanation of the algorithm, check out this excellent article:
- โก Speed Control: Adjust animation speed from 1ms to 2000ms
- ๐ฎ Keyboard Shortcuts:
โโPrevious/Next stepSpacePlay/PauseHomeFirst stepEndLast step
- ๐ฏ Color-Coded Bars: Blue โ Green โ Yellow โ Red (small to large values)
- ๐ Real-time Stats: Current step and operation display
- Run the program with the
-vflag to record each operation - All steps are serialized to JSON and embedded in HTML
- An HTML file is automatically generated and opened in your browser
- Enjoy the Matrix rain effects in the background ๐ฅ
| Element Count | Average Moves | Max Moves |
|---|---|---|
| 3 | 2-3 | 3 |
| 5 | 8-12 | 12 |
| 100 | ~550 | <700 |
| 500 | ~3500 | <5500 |
GoSortStack/
โโโ cmd/
โ โโโ main.go # Main program entry point
โโโ internal/
โ โโโ algorithm/ # Sorting algorithms
โ โ โโโ sort.go # Main sort function
โ โ โโโ sort_utils.go # Cost calculation utilities
โ โ โโโ sort_three.go # 3-element sort
โ โ โโโ sort_five.go # 5-element sort
โ โโโ checker/ # Sorted state checker
โ โโโ operations/ # Stack operations
โ โโโ parser/ # Input parsing
โ โโโ stack/ # Stack data structure
โ โโโ visualizer/ # Visualization engine
โโโ visualizer.html # Web visualizer template
โโโ README.md
Visual representation of function calls and dependencies:
Operations aren't printed to terminal when visualizer is active:
if !o.Silent {
fmt.Println("SwapA")
}No CORS issues - JSON is directly embedded in HTML:
const EMBEDDED_DATA = {...};Continuously flowing 0s and 1s in the background โก
# 100 random numbers
./push_swap.exe -v $(shuf -i 1-100 -n 100 | tr '\n' ' ')
# 500 random numbers
./push_swap.exe $(shuf -i 1-500 -n 500 | tr '\n' ' ')./push_swap.exe 5 2 8 1 9 3 7 | wc -lgo test ./...go build -o push_swap.exe cmd/main.goEdit visualizer.html - the program automatically uses it.
โ ๏ธ Duplicate numbers are not acceptedโ ๏ธ Only integer values allowedโ ๏ธ Empty input returns an error- โ Negative numbers are supported
- โ Maximum value: 2147483647 (INT_MAX)
- โ Minimum value: -2147483648 (INT_MIN)
- Implementing linked lists in Go
- Cost-based optimization algorithms
- Web-based visualization techniques
- Integrating Go with HTML/CSS/JavaScript
- Efficient sorting strategies
This project is a modern Go implementation of the classic C Push Swap project. The algorithm is based on the Turk Algorithm.
Special thanks to:
- Ali Ogun for the excellent algorithm explanation and reference implementation
- Medium Article - Comprehensive algorithm breakdown
This project is open source and available under the MIT License.
Made with โค๏ธ and lots of โ
Sort with style - Matrix rain edition! ๐


