This project has been created as part of the 42 curriculum by sarfreit.
push_swap is a project from 42 School that focuses on sorting algorithms and
data manipulation.
The objective is to sort a list of integers in ascending order using two stacks and a limited set of allowed operations, while outputting the smallest possible sequence of instructions.
The program works with:
- Stack A, which initially contains the integers.
- Stack B, which starts empty and is used as auxiliary storage.
-
sa : swap the first two elements of stack A
-
sb : swap the first two elements of stack B
-
ss : sa and sb at the same time
-
pa : push the top element of stack B to stack A
-
pb : push the top element of stack A to stack B
-
ra : rotate stack A (first element becomes last)
-
rb : rotate stack B
-
rr : ra and rb at the same time
-
rra : reverse rotate stack A (last element becomes first)
-
rrb : reverse rotate stack B
-
rrr : rra and rrb at the same time
The program displays:
"Error\n"
and exits if:
- An argument is not a valid integer
- A number overflows or underflows an
int - Duplicate values are provided
All allocated memory is freed before exiting.
To compile the program, run:
makeThis will generate the executable:
./push_swapThe program takes a list of integers as arguments:
./push_swap 3 2 1It outputs the operations required to sort the stack. For example:
sa
rraTo verify if the output of push_swap correctly sorts the stack, use the checker.
It returns simply OK or KO.
./push_swap 3 2 1 | ./checkers/checker_linux 3 2 1Or
ARG="5 4 3 2 1"; ./push_swap $ARG | ./checkers/checker_linux $ARGOr...best of all...a random number generator ! !
ARG=$(shuf -i 0-1000 -n 100 | tr '\n' ' ')
./push_swap $ARG | ./checkers/checker_linux $ARGYou can also check the amount of operations performed to sort ARG by adding "wc -l"
ARG="5 4 3 2 1"; ./push_swap $ARG | wc -lAnd finally, get an average of moves made
total=0
for i in {1..20}; do
ARG=$(shuf -i 0-1000 -n 100 | tr '\n' ' ')
n=$(./push_swap $ARG | wc -l)
echo $n
total=$((total+n))
done
echo "Average: $((total/20))"
670
674
...
690
655
677
Average: 664If the input is already sorted, the program produces no output.
valgrind --leak-check=full --show-leak-kinds=all ./push_swapvalgrind --leak-check=full --show-leak-kinds=all ./push_swap_bonus- 42 School subject:
push_swap - The following website: https://medium.com/@ulysse.gks/push-swap-in-less-than-4200-operations-c292f034f6c0
- Manual pages for stack manipulation and sorting concepts
- Official 42 documentation and peer discussions
- Use of AI to clarify pointer manipulation and linked list logic