The program push_swap will sort random integers on a stack, with a limited set of instructions, trying to use the least stack operations to get an ascending sorted stack.
- Clone the repo
- Call
make release
- Start program with your own input:
./push_swap [put your args seperated by spaces]
- Start program with 500 random ints:
./push_swap `ruby -e "puts (0..499).to_a.shuffle.join(' ')"`
The game is composed of 2 stacks named A and B.
Starting Point:
- Stack_A contains random numbers of either positive or negative numbers without any duplicates.
- Stack_B is empty
The goal is to sort in ascending order numbers into stack.
The operations the program can do, are limited to the following set:
Operations | Description |
---|---|
sa | swap a - swap the first 2 elements at the top of stack a. Do nothing if there is only one or no elements) |
sb | swap b - swap the first 2 elements at the top of stack b. Do nothing if there is only one or no elements) |
ss | sa and sb at the same time |
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 |
ra | rotate a - shift up all elements of stack a by 1. The first element becomes the last one |
rb | rotate b - shift up all elements of stack b by 1. The first element becomes the last one |
rr | ra and rb at the same time |
rra | reverse rotate a - shift down all elements of stack a by 1. The last element becomes the first one |
rrb | reverse rotate b - shift down all elements of stack b by 1. The last element becomes the first one |
rrr | rra and rrb at the same time |
*Visualized with the great push_swap visualizer from o-reo. I really appreciate this visualiser, it helped a lot to find strange behaviour of the sorting algorithms.
*All 42 projects must be written in C (later C++) in accordance to the 42 School Norm.
- All variables have to be declared and aligned at the top of each function
- Each function can not have more then 25 lines
- Projects should be created with allowed std functions otherwise it is cheating