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.
git clone git@github.com:mazen-tech/push_swap.gitcd push_swap/push_swap
makeIf you have problems running the
./checker, usechmod 777 ./checkerand try again.
./push_swap [numbers] | ./checker [numbers]
./push_swap 9 0 -217 2147483647 -2147483648 | ./checker 9 0 -217 2147483647 -2147483648ARG=["numbers"]; ./push_swap $ARG | ./checker $ARG
ARG="3 0 9 2 -1"; ./push_swap $ARG | ./checker $ARGmake - 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:saandsbat 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:raandrbat 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:rraandrrbat 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+\nif 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 -2147483649Should 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
KOorOK.
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;
- ...