- ✅ = Okay project
- ⭐ = Outstanding project
Push_swap is a sorting program that aims to efficiently sort data on a stack using a limited set of instructions while minimizing the number of actions. This project offered me the chance to manipulate various sorting algorithms and choose the most suitable solution for an optimized data sorting algorithm.
- Stacks: The program operates with two stacks, namely
a
andb
. These two stacks could be implemented as dynamically allocated arrays or as linked lists. - Initial State:
- Stack
a
contains a collection of negative and/or positive numbers, with no duplicates. - Stack
b
starts empty.
- Stack
- Objective: Sort the numbers in stack
a
in ascending order. - Available Sorting Operations:
sa
(swap a): Swap the first two elements at the top of stacka
.sb
(swap b): Swap the first two elements at the top of stackb
.ss
: Performsa
andsb
simultaneously.pa
(push a): Move the first element at the top of stackb
to the top of stacka
.pb
(push b): Move the first element at the top of stacka
to the top of stackb
.ra
(rotate a): Shift all elements of stacka
up by one, making the first element the last.rb
(rotate b): Shift all elements of stackb
up by one, making the first element the last.rr
: Performra
andrb
simultaneously.rra
(reverse rotate a): Shift all elements of stacka
down by one, making the last element the first.rrb
(reverse rotate b): Shift all elements of stackb
down by one, making the last element the first.rrr
: Performrra
andrrb
simultaneously.
To use the push_swap program, follow these instructions:
- Compile the program.
- Provide a list of integers as arguments to the program. The first argument should be at the top of stack
a
.
./push_swap 4 2 7 1 3
- The program will display the smallest list of instructions needed to sort the stack a, with the smallest number at the top.
sa
ra
...
- Each instruction is separated by a newline (\n).
- If no parameters are specified, the program will not display anything and return the prompt.
- In case of errors, such as non-integer arguments, values exceeding integer limits, or duplicate values, the program will display an "Error" message followed by a newline (\n) on the standard error.