Don`t forget to give it a ⭐Star.
Push_swap is a sorting algorithm implemented in C. It efficiently sorts a stack of intengers using a series of predefined operations.
Before reviewing my code, I strongly advise against analyzing it in its current state. It's quite disorganized, reflecting my early attempts at tackling the Pushswap project. While it may not be a masterpiece of code, it serves as a starting point for my learning journey.
Don't worry, though. At the end of this file, I'll share some solutions that helped me successfully complete the project. Additionally, I've included detailed documentation that I found to be very helpful in understanding and improving my code.
The project objectives ✍️
Basicaly we have two stacks (don`t worry if you don`t understand any concept or word yet, they`ll be explained later on) A and B then we gonna create a program that implemnts a sorting algorithm which will use those two stacks to sort the stacks.
Stacks | A | B |
---|---|---|
-38 | ||
10 | ||
7 | ||
42 |
Our program works this way:
./push_swap -38 10 7 42
./push_swap
is our program
38 10 7 42
is our input
./push_swap
take this input, use the implemented sorting algorithm and return a value (this value is the amount of used operations to sort the values)
- what are these operations ?
We have a pre-set of operations to use. they are:
- swap
- rotate
- reverse rotate
- push
Well. Now thigs will start make some sense. Let`s see step by step.
remember these numbers 38 10 7 42
let`s see our program working and you will understand why we started talking about all of that.
let`s run our program
./push_swap 38 10 7 42
pb
sa
pa
pb, sa, pa?
Stacks | A | B |
---|---|---|
-38 | ||
10 | ||
7 | ||
42 |
- pb stands for push B so our first operation in our program is push our first value to stack b
Stacks | A | B |
---|---|---|
-> | -38 | |
10 | ||
7 | ||
42 |
- Next operation is sa which is swap A. Gonna swap the first two items of stack A:
Before Swap A | After Swap A | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
- Now lets see our last operation happening; PA which stands for Push A
Before Push A | After Push A | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Our stack A at the beginning was
-38 10 7 42
Now is
-38 7 10 42
It is in ascending order! that`s what we wanted to have.
Now go back to the beginning of our program running to wach again each step.
Well, now you need to start code. What i recommend: Start with the stack_a creation, receive the args, transform them into values and place into the stack - I did this using linked list (to be honest i don`t know yet if there is another solution than linked lists).
Its everything working properly?
- Yes:
So start building te functions in the subject (push, swap, rotate [...]) and don't forget to test them a lot with your stack_a. That's a good starting point and it will take a lot of time to do all of that, but you`ll understand the main concept of Push_swap after that.
- Our program will receive intengers.
- will take these intengers and use both stacks A and B to put them in ascending order
- will use the operations provided in the subject (pa, pb, sa, sb...) to order them.
This sould give you a god starting point to create your own push_swap. Remember, you wi`ll implement a sorting algorithm to use both stacks do what you have to. Try by your own, then come back here i you need to watch the next steps. I will update this README to help everyone. don not forget to give me a ⭐Star