This project has been created as part of the 42 curriculum by mozay, sumdogan.
Push_swap is a sorting algorithm project that sorts data on a stack using a limited set of instructions with the lowest possible number of actions.
The program takes a list of integers as input and outputs a sequence of operations that sorts the stack in ascending order.
The challenge lies in sorting the numbers efficiently using only two stacks (stack a and stack b) and the following restricted operations:
sa, sb, ss -> Swap operations
pa, pb -> Push operations
ra, rb, rr -> Rotate operations
rra, rrb, rrr -> Reverse rotate operations
The project demonstrates understanding of:
- Algorithm complexity
- Data structures
- Optimization techniques
- Low-level program design
The bubble sort algorithm repeatedly steps through the stack, compares adjacent elements, and swaps them if they are in the wrong order. This process continues until the stack is fully sorted.
This algorithm serves as a baseline implementation.
It is:
- Easy to implement
- Easy to understand
- Suitable for very small stacks (≤5 elements)
The quadratic complexity is acceptable for such small inputs and avoids the overhead of more complex algorithms.
The stack is divided into √n sized chunks based on element indices.
Steps:
- Elements are pushed to stack b chunk by chunk.
- After all chunks are transferred, elements are returned to stack a.
- The algorithm always moves the maximum element in stack b to the top before pushing it back.
This approach:
- Reduces the number of operations compared to O(n²)
- Keeps implementation complexity reasonable
- Works well for medium sized stacks (6–100 elements)
This algorithm simulates a heap-like selection strategy:
- Find the maximum element in stack a
- Rotate stack
auntil the element reaches the top - Push it to stack b
- Repeat until stack
ais empty - Push all elements back from b → a
Occasional swaps are used to maintain correct order.
In the push_swap model, operation count is the main cost.
This approach:
- Reduces total operations
- Performs well for large inputs (100+ elements)
The algorithm first calculates a disorder metric of the stack:
0 = already sorted
1 = reverse sorted
Based on this metric, the program automatically selects the best strategy.
| Disorder Level | Algorithm Used | Complexity |
|---|---|---|
| < 0.2 | Bubble Sort | O(n²) |
| 0.2 – 0.5 | Block-Based Sort | O(n√n) |
| ≥ 0.5 | Radix Sort Adaptation | O(n log n) |
No single algorithm performs optimally for every input.
Adaptive selection allows:
- Faster sorting for nearly sorted stacks
- Better performance for highly disordered stacks
- Improved average-case efficiency
- ✅ Multiple Sorting Strategies – Four algorithms with different complexity classes
- ✅ Runtime Strategy Selection – Choose algorithms using command-line flags
- ✅ Benchmark Mode – Detailed operation statistics with
--bench - ✅ Disorder Calculation – Automatic measurement of input disorder
- ✅ Error Handling – Validation for invalid inputs
- ✅ Memory Management – Proper allocation and deallocation
Input validation includes:
- Non-integer values
- Duplicate numbers
- Out-of-range values
- CC Compiler
- Make
- Unix-like OS (Linux / macOS)
# Compile the program
make
# Remove object files
make clean
# Remove object files and executable
make fclean
# Recompile from scratch
make reAfter compilation, the executable push_swap will be created.
# Pass numbers as separate arguments
./push_swap 5 2 8 1 9
# Pass numbers as a single quoted string
./push_swap "5 2 8 1 9"
# Use a variable
ARG="5 2 8 1 9"; ./push_swap $ARG# Simple algorithm
./push_swap --simple 5 2 8 1 9
# Medium algorithm
./push_swap --medium 5 2 8 1 9
# Complex algorithm
./push_swap --complex 5 2 8 1 9
# Adaptive algorithm (default)
./push_swap --adaptive 5 2 8 1 9# Run with benchmark statistics
./push_swap --bench 5 2 8 1 9
# Save benchmark output
./push_swap --bench 5 2 8 1 9 2> bench.txt
# Combine with strategy selection
./push_swap --bench --complex 5 2 8 1 9ARG="5 2 8 1 9"
./push_swap $ARG | ./checker_linux $ARGBenchmark and verify:
ARG="5 2 8 1 9"
./push_swap --bench $ARG 2> bench.txt | ./checker_linux $ARGARG=$(shuf -i 0-9999 -n 100 | tr '\n' ' ')
./push_swap $ARG | wc -l./push_swap --bench $ARG 2> bench.txt
cat bench.txtARG=$(shuf -i 0-9999 -n 500 | tr '\n' ' ')
echo "Operation count: $(./push_swap $ARG | wc -l)"$ ./push_swap 5 2 8 1 9
pb
pb
ra
pa
pa
$ ./push_swap --bench 5 2 8 1 9
pb
pb
ra
pa
pa
[bench] disorder: 40.00%
[bench] strategy: Adaptive / O(n√n)
[bench] total_ops: 5
[bench] sa: 0 sb: 0 ss: 0 pa: 2 pb: 2
[bench] ra: 1 rb: 0 rr: 0 rra: 0 rrb: 0 rrr: 0
Duplicate values:
$ ./push_swap 5 2 8 1 2
Error
Out-of-range values:
$ ./push_swap 5 2 8 1 2147483648
Error
Invalid input:
$ ./push_swap 5 2 8 1 "abc"
Error
- 42 School Norm
- Push_swap Subject
- Big O Notation
- Sorting Algorithms Visualized
- Stack Data Structure
- Bubble Sort
- Heap Sort
- Push_swap Visualizer
- Checker for Linux
Artificial intelligence tools were used ethically and responsibly during development.
AI assistance included:
- Chunk size optimization for block sort
- Heap-based element selection strategies
- Disorder threshold tuning
- Splitting functions to comply with 42 Norm (≤25 lines)
- Reducing parameter counts
- Improving modular file organization
- Memory leak detection
- Edge case identification
- Operation sequence optimization
AI helped generate:
- README structure
- Documentation text
- Code comments
| Name | Role | Contributions |
|---|---|---|
| mozay | Developer | Algorithm implementation, stack operations, error handling ,bench operations |
| sumdogan | Developer | Algorithm implementation,arguman checks , testing , memory managements |
Both contributors participated in all stages of development and fully understand the entire codebase.