This is a very simple and highly effective algorithm project:
A set of integer wants to be sorted using only 2 stacks and a limited set of instructions to manipulate both stacks.
The goal?
Write 2 programs in C:
- The first, named
checker
, takes integer arguments and reads instructions on the standard output. Once read,checker
executes them and displaysOK
if integers are sorted. Otherwise, it will displayKO
. - The second one, called
push_swap
, calculates the smallest amount of push-swap instruction language operations that sorts integer arguments received and displays them on the standard output.
sa
(swap a): swap the first 2 elements at the top of stack a. Do nothing if there is only one or no elements)sb
(swap b): swap the first 2 elements at the top of stack b. Do nothing if there is only one or no elements)ss
:sa
andsb
at the same timepa
(push a): take the first element at the top of b and put it at the top of a. Do nothing if b is emptypb
(push b): take the first element at the top of a and put it at the top of b. Do nothing if a is emptyra
(rotate a): shift up all elements of stack a by 1. The first element becomes the last onerb
(rotate b): shift up all elements of stack b by 1. The first element becomes the last onerr
:ra
andrb
at the same timerra
(reverse rotate a): shift down all elements of stack a by 1. The last element becomes the first onerrb
(reverse rotate b): shift down all elements of stack b by 1. The last element becomes the first one.rrr
:rra
andrrb
at the same time
At the root of the repository run make
.
define a variable ARG
as the set of integers to be sorted. For example:
$> ARG="4 6 2 7 8 15"
$> ./push_swap [options] $ARG
options:
-v verbose
-c color
$> ./push_swap $ARG | ./checker $ARG