"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 it's necessary to manipulate various
types of algorithms and choose the most appropriate solution (out of many) for an
optimized data sorting."
- Introduction
- Usage
- Sorting Algorithm
- Bonus
- Bonus Usage
- Bonus Behaviour
- Makefile Overview
- License and Thoughtful Advice
The Push Swap project presents a simple yet essential algorithmic task: sorting data efficiently. In this project, we work with a collection of integer values, two stacks, and a set of instructions to manipulate these stacks. Our objective is to develop a C program named push_swap that determines and outputs the shortest sequence of Push Swap language instructions required to sort the given integers.
The code was written according to the 42 norm guidelines(norminette)
- On your terminal, clone the repository
git clone git@github.com:amauricoder/42_Push_Swap.git
- Considering that you already have cloned the repository, do make bonus
make bonus
This will generate a checker program in your bonus folder, this is the executable of the bonus program.
- Run the program with the numbers you want as parameters
./push_swap 5 -10 13
This will return the moviments necessary to organize the stack.
To know more about the rules for moviments, see Subject
This code was written based on an article by A. Yigit Ogun. Click Here if you want to check it out.
A. Yigit Ogun called this "The Turc Algorithm". Essentially, he determines the optimal sequence of moves required to arrange a set of numbers in order, and then performs the sorting accordingly. This method prioritizes efficiency by minimizing the number of moves needed to put the stack in order.
First of all: for the turc algorithm to work we need to have more than 3 numbers on the stack. If we have 3 numbers only, we use a simple sort three algorithm, that is based on all the exiting possibilities to sort 3 numbers. If we have only 2 numbers, then we use a simple "sa", that changes the position of the first and second number of stack a.
Again, to know more about the moviments and the rules, please check Subject.
If we have more than 3 numbers, the turc algorithm takes action.
Explaining in a very simple and direct form how it works:
- Transfer all elements from STACK_A to STACK_B in descending order. This rearrangement is intended to facilitate automatic sorting when the elements are subsequently pushed back to STACK_A.
- We perform the transfer from STACK_A to STACK_B until STACK A contains only three numbers.
- When STACK_A is only with the 3 numbers, we perform a sort three algorithm on STACK_A and send the numbers of STACK_B back to STACK_A.
For a more detailed explanation, I highly recommend for you too read the article Here.
Below, there is an illustration of how this algorithm works.
If you want to test the program like this, check the push_swap visualizer.
The bonus requires the creation of a program that reproduces the behaviour of the checker, but not 100% equally. The checker is a program that is given by the 42 school to check if the numbers are sorted.
- On your terminal, clone the repository
git clone git@github.com:amauricoder/42_Push_Swap.git
- Considering that you already have cloned the repository, do make bonus
make bonus
This will generate a checker file in the bonus folder, this is the executable of the bonus program.
- Run the bonus program with the numbers you want as parameters (Please, check bonus behaviour topic below for more details about the usage of this program).
./bonus/checker 5 -10 13
Upon executing the program, you'll be prompted to input valid movements via the terminal. These movements will determine whether the stack becomes organized as a result.
If the moviment organizes the stack, it will give you an "Ok" as result. If not, it will give you an "KO". If the input is invalid, it will give you an "Error".
Since this bonus part we were asked to create a checker, but not 100% equal to the original checker, I've decided to put some extra features on it.
To access these features, use the following flags on the execution.
-s | -c |
---|---|
print the stacks | print in color the last moviment that took action |
Examples of usage of the checker program below:
Normal Usage
Using -s flag
Using -c flag alone and -s and -c together
In this project, the Makefile offers the following essential rules:
- make: Compiles the project to push_swap on root folder.
- make clean: Cleans the directory by removing
.o
files, preservingpush_swap
executable. - make fclean: Completely cleans the directory by deleting both
.o
files andpush_swap
. - make re: Refreshes
push_swap
by recompiling everything. - make valgrind: Checks for memory leaks(Valgrind program required).
- make bonus: Compiles the bonus part to checker on the bonus folder.
- make bonus_clean: Clean the bonus
.o
files, preserving the checker executable. - make bonus_fclean: Completely cleans the bonus directory by deleting both
.o
files andchecker
.
Stop deceiving yourself. Mere ability to copy code doesn't enhance your skills. This is outside the scope of school purposes. Everyone can be a "copy paster," but not everyone can be a programmer. Good luck!