This project has been created as part of the 42 curriculum by aboussab.
Push Swap is a sorting project that challenges you to sort a list of integers using only two stacks (A and B) and a limited set of operations, while minimizing the total number of moves. The program takes numbers as input, validates them, builds stack A, and outputs a sequence of operations that sorts the data in ascending order.
- Goal: produce the shortest possible sequence of moves to sort stack A.
- Model: two stacks (A as the source, B as auxiliary).
- Output: only the operations, one per line.
- Allowed operations: sa (swap a): Swap the first 2 elements at the top of stack a. Do nothing if there is only one element or none. sb (swap b): Swap the first 2 elements at the top of stack b. Do nothing if there is only one element or none. ss : sa and sb at the same time. pa (push a): Take the first element at the top of b and put it at the top of a. Do nothing if b is empty. pb (push b): Take the first element at the top of a and put it at the top of b. Do nothing if a is empty. ra (rotate a): Shift up all elements of stack a by 1. The first element becomes the last one. rb (rotate b): Shift up all elements of stack b by 1. The first element becomes the last one. rr : ra and rb at the same time. rra (reverse rotate a): Shift down all elements of stack a by 1. The last element becomes the first one. rrb (reverse rotate b): Shift down all elements of stack b by 1. The last element becomes the first one. rrr : rra and rrb at the same time.
To test or use this programme you clone the main repository then use the cmd make to compile it and get the "./push_swap" this is our programe now all you have to do is give it some numbers to test it as bellow :
./push_swap 3 2 1
./push_swap 1 5 2 4 3
ARG=$(seq 1 100 | shuf | tr '\n' ' '); ./push_swap $ARG | wc -l
ARG=$(seq 1 500 | shuf | tr '\n' ' '); ./push_swap $ARG | wc -l
I used AI minimally: to better understand the concept and subject, and to help draft this README. No algorithms or project code were generated by AI.