A project designed to teach sorting algorithms and their complexity by implementing a stack-based sorting program in C.
The Push_swap project challenges you to sort data on a stack using a limited set of instructions with the fewest possible operations. The goal is to write a C program that efficiently sorts a list of integers and outputs the sequence of sorting instructions.
- Implements various stack-based sorting algorithms.
- Handles random positive and negative integers with no duplicates.
- Utilizes a minimal number of operations.
- Provides a
checker
program to verify the sorting sequence.
The following operations are available for manipulating two stacks, a
and b
:
sa
: Swap the first two elements of stacka
.sb
: Swap the first two elements of stackb
.ss
: Swap the first two elements of both stacks simultaneously.pa
: Push the top element of stackb
to stacka
.pb
: Push the top element of stacka
to stackb
.ra
: Rotate stacka
(shift all elements up by one).rb
: Rotate stackb
.rr
: Rotate both stacks simultaneously.rra
: Reverse rotate stacka
(shift all elements down by one).rrb
: Reverse rotate stackb
.rrr
: Reverse rotate both stacks simultaneously.
Consider sorting the integers 2 1 3 6 5 8
. An example sequence of operations might be:
ra
pb
ra
ra
pb
rra
pa
rra
rra
pa
- Compile the program using the provided
Makefile
:
make
Run the push_swap program with a list of integers:
./push_swap 2 1 3 6 5 8
Verify the sorting result with the checker program (Bonus):
ARG="2 1 3 6 5 8"; ./push_swap $ARG | ./checker $ARG