Sort a stack of integers using only a second stack and a restricted instruction set — the goal is the smallest possible number of operations.
Language: C (-Wall -Wextra -Werror) · Context: 42 School — solo
sa sb ss · pa pb · ra rb rr · rra rrb rrr
make
./push_swap "4 67 3 87 23"
ARG="5 2 8 1 9 3"; ./push_swap $ARG | wc -l # count moves
ARG="…"; ./push_swap $ARG | ./checker_linux $ARG # verify (OK / KO)- Small inputs (≤ 5) are handled by hand-written optimal sequences.
- Larger inputs use a cost-driven greedy approach:
- numbers are replaced by their rank (index), so the values themselves stop mattering;
- everything is pushed to stack B;
- for each element of B, the code computes the cost of bringing it to the
right place in A — counted as rotations on each side, using the combined
rr/rrrmoves whenever both stacks rotate the same way; - the cheapest element is moved, and the step repeats until B is empty;
- a final rotation puts the smallest element of A on top.
The project's top grading band: < 700 moves for 100 numbers, < 5500 for 500.
- Input parsing rejects non-integers, overflow, and duplicates before any work starts.
- All allocations are freed on every error path.