Push_Swap is a sorting algorithm project that aims to sort a stack of integers using a limited set of operations. The project is written in C and is a part of the curriculum at School 42SP.
The goal of Push_Swap is to take an unsorted stack of integers and sort it in ascending order using two stacks and a set of predefined operations. The operations allowed are:
sa
: swap the first two elements of stack Asb
: swap the first two elements of stack Bss
: dosa
andsb
simultaneouslypa
: push the first element of stack B to stack Apb
: push the first element of stack A to stack Bra
: rotate all elements of stack A by one position upwardsrb
: rotate all elements of stack B by one position upwardsrr
: dora
andrb
simultaneouslyrra
: rotate all elements of stack A by one position downwardsrrb
: rotate all elements of stack B by one position downwardsrrr
: dorra
andrrb
simultaneously
The program takes a list of integers as arguments and outputs a series of operations that, when executed, will sort the stack in the fewest possible moves.
To compile the program, run the following command:
make
To execute the program, run the following command:
./push_swap [list of integers]
For example:
./push_swap 5 2 1 3 4
The program will output a series of operations that can be used to sort the stack.
Push_Swap uses a combination of different sorting algorithms based on the number of elements in the stack. For small stacks (less than 3 elements), it employs a simple sorting algorithm. For larger stacks, it uses a more complex algorithm that divides the stack into smaller chunks and applies various operations to sort them.
Feel free to explore the Push_Swap repository for the full source code and additional information.