This project has been created as part of the 42 curriculum by hrasamoe and nrasamis.
push_swap sorts a stack of integers using two stacks (A and B) and a limited set of operations.
Goal: Sort stack A in ascending order using the fewest operations possible.
This implementation features an adaptive engine that evaluates input disorder and automatically selects the most efficient sorting strategy.
You start with:
- Stack A — contains all integers (unsorted)
- Stack B — empty (auxiliary stack)
| Operation | Description |
|---|---|
sa |
Swap top 2 elements of A |
sb |
Swap top 2 elements of B |
ss |
sa + sb |
pa |
Push top of B to A |
pb |
Push top of A to B |
ra |
Rotate A (top → bottom) |
rb |
Rotate B (top → bottom) |
rr |
ra + rb |
rra |
Reverse rotate A (bottom → top) |
rrb |
Reverse rotate B (bottom → top) |
rrr |
rra + rrb |
The program prints each operation to stdout, one per line.
git clone <repository_url>
cd push_swap
makemake clean
make fclean
make re./push_swap 4 2 7 1 9 3./push_swap "4 2 7 1 9 3"./push_swap "4 2 7" 1 9 3| Flag | Description |
|---|---|
--bench |
Print performance stats to stderr |
--simple |
Force selection sort |
--medium |
Force chunk sort |
--complex |
Force radix sort |
--adaptive |
Default automatic strategy |
./push_swap --complex --bench 4 2 7 1 9 3./push_swap 1 2 3 4 5 # Already sorted → no output
./push_swap 42 # Single element → no output
./push_swap -3 0 5 -1 2 # Supports negatives./push_swap 1 2 a 3 # Invalid input
./push_swap 1 2 2 3 # Duplicate
./push_swap 99999999999 # OverflowPrints Error to stderr
- https://www.geeksforgeeks.org/dsa/radix-sort/
- https://push.eliotlucas.ch/
- https://www.guru99.com/fr/bucket-sort.html
AI (Claude) was used for:
- Algorithm explanations
- Refactoring guidance
- Debugging memory issues
- Documentation writing
disorder < 0.2 → selection sort O(n²)
disorder < 0.5 → chunk sort O(n√n)
disorder ≥ 0.5 → radix sort O(n × k)
- N ≤ 5 → optimized cases
- N > 5 → repeatedly push minimum to B
Concept:
Find min → push to B → repeat → push back to A
Complexity: O(n²)
Best for medium-sized inputs (~100 elements).
Steps:
- Normalize values (assign ranks)
- Divide into chunks (
√N) - Push chunk by chunk to B
- Rebuild A by pushing max from B
⏱ Complexity: O(n√n)
Best for large inputs.
Steps:
-
Normalize values (ranking)
-
Process bits from LSB to MSB:
- bit = 0 →
pb - bit = 1 →
ra
- bit = 0 →
-
Push back from B to A
Complexity: O(n × k) (k = number of bits)
Measures how shuffled the input is:
disorder = inversions / (N × (N - 1) / 2)
0.0→ sorted1.0→ reverse sorted0.5→ random
Complexity: O(n²)
./push_swap --bench $(cat args.txt)Example output:
[bench] disorder: 0.62
[bench] strategy: radix_sort
[bench] total_ops: 4872
./push_swap $(cat args.txt) | ./checker_linux $(cat args.txt)Count operations:
./push_swap $(cat args.txt) | wc -l- Debugging
- Testing (large datasets)
- Code review
- Checker integration
- Algorithms (simple & complex)
- Parsing & validation
- Makefile
- Benchmark system
- Stack operations
- Medium algorithm & disorder metric
- Adaptive engine
- Header (
push_swap.h) - README