This project has been created as part of the 42 curriculum by yaboualk, yfarhat.
Push_swap is an algorithm project from the 42 curriculum. The goal is to sort a list of integers using two stacks, a and b, and a limited set of operations, while using the smallest possible number of moves.
At the start, stack a contains the numbers (no duplicates) and stack b is empty.
The program push_swap does not print the sorted numbers.
It prints the list of operations that sorts stack a in ascending order, with the smallest number on top.
| Operation | Effect |
|---|---|
sa, sb, ss |
Swap the first two elements of a, b, or both |
pa, pb |
Push the top element from b to a, or from a to b |
ra, rb, rr |
Rotate a, b, or both (the top element goes to the bottom) |
rra, rrb, rrr |
Reverse rotate a, b, or both (the bottom element goes to the top) |
The project is about understanding algorithmic complexity in a concrete way. Sorting numbers is easy, but sorting them quickly with only two stacks and these moves is much harder. The complexity of each algorithm is measured by the number of operations it prints.
Our program contains four strategies:
- Simple, O(n²): a basic algorithm, easy to understand.
- Medium, O(n√n): a chunk-based algorithm.
- Complex, O(n log n): a radix-based algorithm.
- Adaptive: measures the disorder of the input (a value from 0 to 1) before any move, then chooses the best method for it. This is the default behavior.
The program also has a --bench mode that prints the disorder, the strategy used, and the number of each operation to the standard error.