About • How to use • Mandatory • Bonus • Norminette • Contributing • License
In this project, I developed a sorting algorithm for a specific problem using two stacks. The goal of the project is to efficiently sort a stack of integers using a set of predefined operations.
- Subject
PDF
- References
GitHub
git clone git@github.com:jotavare/push_swap.git
cd push_swap/push_swap
make
If you have problems running the
./checker
, usechmod 777 ./checker
and try again.
./push_swap [numbers] | ./checker [numbers]
./push_swap 9 0 -217 2147483647 -2147483648 | ./checker 9 0 -217 2147483647 -2147483648
ARG=["numbers"]; ./push_swap $ARG | ./checker $ARG
ARG="3 0 9 2 -1"; ./push_swap $ARG | ./checker $ARG
make
- Compile push_swap mandatory functions.
make bonus
- Compile push_swap bonus functions.
make all
- Compile mandatory + bonus functions.
make clean
- Delete all .o (object files) files.
make fclean
- Delete all .o (object files) and .a (executable) files.
make re
- Use rules fclean
+ all
.
The program is only allowed to work with two stacks, stack A and stack B. All the numbers are initially added to stack A, and B is empty.
Possible actions:
-
pa
(push A): Take the first element at the top of B and put it at the top of A. Do nothing if B is empty. -
pb
(push B): Take the first element at the top of A and put it at the top of B. Do nothing if A is empty. -
sa
(swap A): Swap the first 2 elements at the top of stack A. Do nothing if there are only one or no elements. -
sb
(swap B): Swap the first 2 elements at the top of stack B. Do nothing if there are only one or no elements. -
ss
:sa
andsb
at the same time. -
ra
(rotate A): Shift all elements of stack A up by 1. The first element becomes the last one. -
rb
(rotate B): Shift all elements of stack B up by 1. The first element becomes the last one. -
rr
:ra
andrb
at the same time. -
rra
(reverse rotate A): Shift all elements of stack A down by 1. The last element becomes the first one. -
rrb
(reverse rotate B): Shift all elements of stack b down by 1. The last element becomes the first one. -
rrr
:rra
andrrb
at the same time.
The grade depends on how efficient the program's sorting process is.
-
Sorting 3 values: no more than 3 actions.
-
Sorting 5 values: no more than 12 actions.
-
Sorting 100 values: rating from 1 to 5 points depending on the number of actions:
- 5 points for less than 700 actions.
- 4 points for less than 900 actions.
- 3 points for less than 1100 actions.
- 2 points for less than 1300 actions.
- 1 point for less than 1500 actions.
-
Sorting 500 values: rating from 1 to 5 points depending on the number of actions:
- 5 points for less than 5500 actions.
- 4 points for less than 7000 actions.
- 3 points for less than 8500 actions.
- 2 points for less than 10000 actions.
- 1 point for less than 11500 actions.
Note: Validating the project requires at least 80/100. I believe 3 points with 100 and 500 numbers would be 80/100.
The program should print
Error
+\n
if the following tests are made:
- Non-numeric parameters.
- Duplicate numeric parameter.
- Numeric parameter greater than INT_MAX.
- Numeric parameter less than INT_MIN.
./push_swap 4 bb 2
./push_swap 4 4 5
./push_swap 4 2 2147483648
./push_swap 4 2 -2147483649
Should not print anything if the following tests are made:
- No parameter.
- Single numeric argument.
- The numbers are already sorted.
./push_swap
./push_swap 42
./push_swap 0 1 2 3
./push_swap 0 1 2 3 4 5 6 7 8 9
- Create a checker for push_swap that will read the program instructions and display
KO
orOK
.
At 42 School, it is expected that almost every project is written following the Norm, which is the coding standard of the school.
- No for, do...while, switch, case, goto, ternary operators, or variable-length arrays allowed;
- Each function must be a maximum of 25 lines, not counting the function's curly brackets;
- Each line must be at most 80 columns wide, with comments included;
- A function can take 4 named parameters maximum;
- No assigns and declarations in the same line (unless static);
- You can't declare more than 5 variables per function;
- ...
- 42 Norms - Information about 42 code norms.
PDF
- Norminette - Tool to respect the code norm, made by 42.
GitHub
- 42 Header - 42 header for Vim.
GitHub
If you find any issues or have suggestions for improvements, feel free to fork the repository and open an issue or submit a pull request.
This project is available under the MIT License. For further details, please refer to the LICENSE file.