A 42 sorting-algorithm project. Given a stack of integers, output the shortest possible sequence of allowed stack operations that sorts them.
You have two stacks, A and B. A starts with the input integers; B starts empty. Sort A in ascending order using only the operations below — and minimize the move count.
| Op | Effect |
|---|---|
sa |
Swap the top two elements of A |
sb |
Swap the top two elements of B |
ss |
sa and sb simultaneously |
pa |
Push the top of B onto A |
pb |
Push the top of A onto B |
ra |
Rotate A upward (top element becomes last) |
rb |
Rotate B upward |
rr |
ra and rb simultaneously |
rra |
Reverse rotate A (last element becomes first) |
rrb |
Reverse rotate B |
rrr |
rra and rrb simultaneously |
Stacks are stored as arrays. The strategy depends on input size:
- ≤ 5 elements — brute-force the optimal sequence
- Larger inputs — a chunk-based / quicksort-style strategy: push partitions of
AtoB, sort there, then merge back
Edge cases handled: empty input, single element, already-sorted input, duplicate-value rejection, and non-integer / overflow input rejection.
make./push_swap 3 2 5 1 4Output is the operation sequence:
pb
pb
ra
pa
pa
Pipe into the 42 checker to verify:
./push_swap 3 2 5 1 4 | ./checker 3 2 5 1 4Or with a variable:
ARG="3 2 5 1 4"; ./push_swap $ARG | ./checker $ARGpush_swap.c,push_swap.h— entry point and headersparsing/— argument parsing, validation, error handlingoperations/— implementations ofsa/sb/ra/rb/pa/pb/rra/rrb/...sorting/— small-case brute force + chunk-based sortlibft/— utility functions