What is Push Swap?
Push Swap is an algorithmic sorting challenge that sorts a collection of integers using two stacks and a constrained set of operations. The goal is to sort the numbers in ascending order on stack A using the minimum number of moves possible.Push Swap is a classic algorithmic problem from the 42 school curriculum that teaches optimization, data structures, and algorithm design.
The Problem
You are given:- Stack A: Contains a random set of integers
- Stack B: Empty stack used as auxiliary storage
- A set of operations: Limited moves to manipulate both stacks
The Turkish Algorithm
This implementation uses the Turkish Algorithm (also known as the Turk Algorithm), an efficient approach for sorting with two stacks. The algorithm works by:- Analyzing costs: Each element is assigned a “push cost” representing the number of operations needed to move it to its correct position
- Finding targets: For each element in stack A, the algorithm identifies its best target position in stack B
- Choosing optimal moves: The algorithm selects the cheapest element to move based on calculated costs
- Optimizing rotations: When both stacks need rotation in the same direction, the algorithm uses simultaneous operations (
rrorrrr)
Video Tutorial
Watch a detailed explanation of the Push Swap algorithm and the Turkish approach
Key Features
Smart Cost Analysis
The implementation calculates the exact cost to move each element by considering:- Position in the current stack (above or below median)
- Target position in the destination stack
- Potential for simultaneous rotations
init_a_to_b.c:64-82:
Optimized for Different Input Sizes
- 2 elements: Single swap operation
- 3 elements: Specialized hardcoded sorting (max 2 operations)
- 4+ elements: Full Turkish algorithm with cost optimization
Input Validation
Robust error handling for:- Invalid syntax (non-numeric characters)
- Integer overflow (outside INT_MIN to INT_MAX range)
- Duplicate numbers
- Empty input
Flexible Input Format
Supports two input methods:Available Operations
The algorithm uses 11 stack operations:| Operation | Description |
|---|---|
sa | Swap first 2 elements of stack A |
sb | Swap first 2 elements of stack B |
ss | sa and sb simultaneously |
pa | Push top of B to A |
pb | Push top of A to B |
ra | Rotate stack A up (first element becomes last) |
rb | Rotate stack B up |
rr | ra and rb simultaneously |
rra | Reverse rotate stack A (last element becomes first) |
rrb | Reverse rotate stack B |
rrr | rra and rrb simultaneously |
Performance
The Turkish algorithm is highly efficient:- 100 numbers: Typically under 700 operations
- 500 numbers: Typically under 5500 operations
Data Structure
The implementation uses a doubly-linked list with rich metadata:Use Cases
- Learning efficient sorting algorithms with constraints
- Understanding algorithm optimization techniques
- Practicing data structure manipulation
- Solving competitive programming challenges
- Teaching algorithmic thinking and cost analysis
Next Steps
Installation
Clone and build the project
Quick Start
Get started with basic usage
Operations Reference
Learn all available stack operations
Algorithm Deep Dive
Understand the Turkish algorithm