An optimized stack-sorting program in C that outputs the minimal (or near-minimal) sequence of allowed operations to sort integers.
push_swap is a well-known 42 School project: given a list of integers, you must sort them using two stacks (A and B) and a limited set of stack operations, printing the operations to stdout.
This repo implements:
- parsing + validation (integers only, no duplicates, int range)
- stack representation and operations
- specialized strategies for small inputs (3/4/5)
- scalable strategies for larger sets (100/500), using indexing / chunking style approaches
Repository: otelliq/push_swap
Default branch: main
- C
- Makefile build
- Custom
libft(vendored inpush_swap/libft)
- Implements all required moves:
- swaps:
sa,sb,ss - pushes:
pa,pb - rotates:
ra,rb,rr - reverse rotates:
rra,rrb,rrr
- swaps:
- Robust input validation:
- checks digits / sign
- prevents overflow (
INT_MIN..INT_MAX) - detects duplicates
- Multiple sorting strategies:
sort_3,sort_4,sort_5sort_100,sort_500(and reverse variants)
(See function prototypes in push_swap/push_swap.h.)
No screenshots provided yet.
A good screenshot to add is a small run example showing the printed operations.
make- C compiler (
cc/clang/gcc)
From the repository root:
make -C push_swapThis should produce the push_swap executable (depending on Makefile rules).
./push_swap 3 2 1Output will be a sequence of operations, e.g.:
sa
rra
If your parser supports it (common in 42 projects):
./push_swap "3 2 1 6 5"If you have a checker tool available (from 42 resources), you can pipe the operations:
ARG="4 67 3 87 23"; ./push_swap $ARG | ./checker $ARG.
└── push_swap/
├── Makefile
├── push_swap.c
├── push_swap.h
├── movements*.c
├── sorting*.c
├── utils*.c
├── errors.c
└── libft/
No license file is currently included in this repository.
If you want this to be usable beyond an academic context, consider adding a LICENSE file (MIT/Apache-2.0 are common choices) and updating this section accordingly.