A Go implementation of the classic push-swap sorting algorithm project. This project implements a non-comparative sorting algorithm using two stacks and a specific set of operations.
push-swap/
├── cmd/
│ ├── push-swap/ # Main push-swap program
│ └── checker/ # Checker program for validation
├── internal/
│ ├── stack/ # Stack data structure implementation
│ ├── operations/ # Stack operations (sa, sb, pa, pb, etc.)
│ ├── parser/ # Input parsing and validation
│ └── solver/ # Sorting algorithm implementation
├── go.mod # Go module file
├── Makefile # Build automation
└── README.md # This file
Calculates and displays the smallest program using push-swap instruction language that sorts the given integer arguments.
Takes integer arguments and reads instructions from standard input. Executes the instructions and displays OK if the integers are sorted correctly, otherwise displays KO.
sa- swap first 2 elements of stack asb- swap first 2 elements of stack bss- execute sa and sbpa- push top element of stack b to stack apb- push top element of stack a to stack bra- rotate stack a (shift up all elements by 1)rb- rotate stack brr- execute ra and rbrra- reverse rotate a (shift down all elements by 1)rrb- reverse rotate brrr- execute rra and rrb
# Build both programs
make build
# Build individual programs
make push-swap
make checker
# Run tests
make test
# Format and vet code
make check
# Clean binaries
make clean# Build push-swap
go build -o push-swap ./cmd/push-swap
# Build checker
go build -o checker ./cmd/checker# Basic usage
./push-swap "4 67 3 87 23"
# Multiple arguments
./push-swap 4 67 3 87 23
# Count operations
./push-swap "4 67 3 87 23" | wc -l# Validate push-swap output
./push-swap "4 67 3 87 23" | ./checker "4 67 3 87 23"
# Manual input
echo -e "sa\npb\npa" | ./checker "3 2 1"
# Interactive mode
./checker "3 2 1"
sa
rra
pb
^D$ ./push-swap "2 1 3 6 5 8"
pb
pb
ra
sa
rrr
pa
pa
$ ./push-swap "0 one 2 3"
Error
$ ./push-swap
(no output)
$ ./checker "3 2 1 0"
sa
rra
pb
KO
$ echo -e "rra\npb\nsa\nrra\npa" | ./checker "3 2 1 0"
OKThe solver uses different strategies based on stack size:
- 2-3 elements: Direct optimal solutions
- 4-5 elements: Optimized small sorting algorithms
- Larger stacks: Chunk-based approach with strategic element positioning
The programs handle various error conditions:
- Invalid integers in input
- Duplicate numbers
- Invalid operations (checker only)
- Empty stacks during operations
Errors are displayed as "Error" followed by a newline on stderr.
Run the test suite:
make testIndividual package tests:
go test ./internal/stack
go test ./internal/operations
go test ./internal/parser# Format code
make fmt
# Run static analysis
make vet
# Run all checks
make check# Run demo with example
make demo
# Validate push-swap output
make validate- Go 1.21 or later
- Standard Go packages only (no external dependencies)
This project is for educational purposes as part of the push-swap algorithm challenge.