MarkosComK / 42-Push_swap

0 stars 0 forks source link

📖 Push_swap

Push_swap

Development repo for 42cursus Push_swap project
For further information about 42cursus and its projects, please refer to 42-Common-Core-Guide repo.

Don`t forget to give it a ⭐Star.

🗣️ About

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)


Operations ? ⚙️

We have a pre-set of operations to use. they are:

ok, but. How do i use all this information ?

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?

first let`s take a look at our starting point (our stacks)

Stacks A B
-38
10
7
42

now we`re going to see each operation of our program(pb, sa, pa)

  1. 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
  1. Next operation is sa which is swap A. Gonna swap the first two items of stack A:
Before Swap AAfter Swap A
| Stacks | A | B | |--------|-------|-------| | | | -38 | | -> | 10 | | | -> | 7 | | | | 42 | | | Stacks | A | B | |--------|-------|-------| | | | -38 | | | 7 | | | | 10 | | | | 42 | |
  1. Now lets see our last operation happening; PA which stands for Push A
Before Push AAfter Push A
| Stacks | A | B | |--------|-------|-------| | | | -38 | | | 7 | | | | 10 | | | | 42 | | | Stacks | A | B | |--------|-------|-------| | | -38 | | | | 7 | | | | 10 | | | | 42 | |

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.

Ok, what now?

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?

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.

recapitulating ✍️

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

Useful resources