This project will make you sort data on a stack, with a limited set of instructions, using the lowest possible number of actions. To succeed you’ll have to manipulate various types of algorithms and choose the one (of many) most appropriate solution for an optimized data sorting.
For more detailed information, look at the subject of this project.
The function is written in C language and thus needs the gcc
compiler and some standard C libraries to run.
1. Compiling the program
To compile, go to the program path and run:
$ make
2. Executing the program
To execute the program, run:
$ ./push_swap $ARG
where $ARG
is a space separated list of integers, e.g. ARG="1 5 2 4 3"
PUSH
Take the first element at the top of one stack and put it at the top of the other; do nothing if the origin stack is empty.
pa
- top element of stack b goes to top of stack a.pb
- top element of stack a goes to top of stack b.
SWAP
Swap the first 2 elements at the top of the stack; do nothing if there is only one or no elements.
sa
- swap stack a.sb
- swap stack b.ss
-sa
andsb
at the same time.
ROTATE
Shift up all elements of the stack by one; the first element becomes the last.
ra
- rotate stack a.rb
- rotate stack b.rr
-ra
andrb
at the same time.
REVERSE ROTATE
Shift down all elements of the stack by one; the last element becomes the first.
rra
- reverse rotate stack a.rrb
- reverse rotate stack b.rrr
-rra
andrrb
at the same time.