This project has been created as part of the 42 curriculum by Kullatida.
My implementation of push_swap for 42 Bangkok. The program sorts unique
integers using two stacks and a limited set of operations. It prints the
instructions needed to sort Stack A in ascending order, with the smallest
value at the top and Stack B empty.
- Accepts separate arguments or numbers inside a quoted argument.
- Rejects duplicate values, invalid numbers, empty arguments, and values
outside the signed
intrange. - Prints
Errorto standard error when input is invalid. - Prints no instructions when no arguments are given or the input is already sorted.
- Implements
sa,sb,ss,pa,pb,ra,rb,rr,rra,rrb, andrrr.
Both stacks use non-circular doubly linked lists. Each node stores its original value, an index, and pointers to the previous and next nodes.
Before sorting, each value receives an index from 0 to n - 1 based on
its rank. This lets the algorithm sort negative and positive values using
non-negative indexes without changing the original numbers.
- 2–3 values: dedicated routines using swaps and rotations.
- 4–5 values: move the smallest values to Stack B, sort the remaining three values, then push the saved values back to Stack A.
- More than 5 values: Binary LSD Radix Sort, processing indexes from
the least significant bit upward. A zero bit triggers
pb; a one bit triggersra. After each pass,pareturns all values to Stack A.
Index assignment takes O(n²) time. The radix sorting stage takes O(n log n) time, so the overall implementation has O(n²) time complexity. The stacks use O(n) memory.
Requires a C compiler and make.
makeThis creates the push_swap executable using -Wall -Wextra -Werror.
./push_swap 5 2 4 1 3
./push_swap "5 2 4 1 3"For example, rotating 3 1 2 once produces 1 2 3:
./push_swap 3 1 2
# Output: raTo count the instructions for a valid input:
./push_swap "5 2 4 1 3" | wc -l| Command | Action |
|---|---|
make |
Build the program |
make clean |
Remove object files |
make fclean |
Remove object files and the executable |
make re |
Clean and rebuild |
The stack operations, input validation, small sorting routines, and radix sort are implemented. No bonus checker is included.
- The 42 push_swap subject
- C manual pages for
write,malloc,free, andexit - References on doubly linked lists, binary numbers, bitwise operators, and Binary LSD Radix Sort
AI tools were used while studying concepts, reviewing edge cases, planning tests, and preparing documentation. Suggestions were reviewed and tested before being used.
Nuun Kullatida — nuunnuun