Push Swap is an efficient sorting algorithm project implemented in C. The goal of this project is to sort a given set of numbers by applying a series of predefined operations. These operations are performed on two stacks: stack_a
and stack_b
. The project showcases the efficiency and effectiveness of the implemented algorithm, adhering to strict coding standards.
The input numbers are initially stored in stack_a
, while stack_b
starts empty. The following operations are allowed to sort the numbers:
sa
: Swap the first two elements ofstack_a
sb
: Swap the first two elements ofstack_b
ss
: Perform bothsa
andsb
pa
: Push the top element ofstack_b
ontostack_a
pb
: Push the top element ofstack_a
ontostack_b
ra
: Rotatestack_a
upwards (the first element becomes the last)rb
: Rotatestack_b
upwards (the first element becomes the last)rr
: Perform bothra
andrb
rra
: Rotatestack_a
downwards (the last element becomes the first)rrb
: Rotatestack_b
downwards (the last element becomes the first)rrr
: Perform bothrra
andrrb
The objective is to sort the numbers in stack_a
using the least number of operations, while following the project's coding requirements.
The sorting algorithm implemented in this project is based on the radix sort, with a few adaptations to manage negative numbers, as the original radix sort can't handle them. The algorithm leverages the concept of index assignments and ensures a constant number of steps when sorting a stack, regardless of the values inside.
The following techniques are used to sort different input sizes:
- For input sizes of 3 or less, a simple sorting function is used to handle the sorting.
- For input sizes between 4 and 6, the
five_sort
function is employed, which moves the smallest elements tostack_b
, sortsstack_a
using the 3-element sorting function, and then moves the elements back tostack_a
. - For larger input sizes, the adapted radix sort algorithm is used, which sorts the numbers based on their indexes, allowing it to handle negative numbers efficiently.
These sorting methods are combined with efficient error handling and memory management to ensure optimal performance and robustness.
To use the Push Swap project, first compile the program using the provided Makefile:
make
Then, run the program with a list of numbers as arguments:
./push_swap 4 3 1 6 8 2
The program will output a series of operations that can be applied to sort the input numbers. The resulting operations can be tested using the provided checker program.
Push Swap is a demonstration of an efficient and elegant sorting algorithm implemented in C, showcasing the power of combining various sorting techniques and effective memory management. By adapting the radix sort algorithm to handle negative numbers and using index assignments, this project offers a practical tool for sorting numbers using a limited set of operations, while maintaining constant sorting steps.