https://github.com/rogeriols/push_swap-42sp
push_swap is a 42 school project where we must sort random numbers with a limited set of instructions, using the lowest possible number of actions.
https://github.com/rogeriols/push_swap-42sp
42 42cursus 42projects 42school c push-swap push-swap42 pushswap pushswap-42 sorting-algorithm sorting-algorithms
Last synced: 16 days ago
JSON representation
push_swap is a 42 school project where we must sort random numbers with a limited set of instructions, using the lowest possible number of actions.
- Host: GitHub
- URL: https://github.com/rogeriols/push_swap-42sp
- Owner: RogerioLS
- License: mit
- Created: 2024-03-16T01:57:16.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-02T15:34:58.000Z (3 months ago)
- Last Synced: 2025-02-13T14:53:33.382Z (2 months ago)
- Topics: 42, 42cursus, 42projects, 42school, c, push-swap, push-swap42, pushswap, pushswap-42, sorting-algorithm, sorting-algorithms
- Language: C
- Homepage:
- Size: 474 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🔃 Push Swap | 42 SP















---
- Actual Status : finished.
- Result : Approved with 100 points ✅---
### 🔷 The Mandatory part:
1. We have at our disposal, two stacks named `a` and `b`.
2. Create a program that takes as parameters, a random set of numbers (negative or positive), without duplicates. Our program has to handle both types of inputs: as a variable number of command line arguments; a string, i.e. "numbers between quotation marks, seperated by a space".
3. Implement an algorithm, that sorts in ascending order, the input of random numbers.
4. Our algorithm will consist of swap, rotate, reverse rotate and push operations.
5. After taking in an input of numbers, and passing them through our sorting algorithms, our program will output the list of operations (instructions).---
### 🔷 Operations:
| Operation | Code | Definition |
| --- | --- | --- |
| **Push** | `pa`, `pb` | The topmost number in one stack is pushed to the top of the other stack. |
| **Swap** | `sa`, `sb`, `ss` | The two topmost numbers in a given stack swap places. `ss` swaps elements in both stacks individually at the same time. |
| **Rotate** | `ra`, `rb`, `rr` | All elements in a given stack are shifted one position up, so that the first element becomes the last one, the second becomes the first, and so on. `rr` rotates both stacks at the same time. |
| **Reverse-rotate** | `rra`, `rrb`, `rrr` | All elements in a given stack are shifted one position below, so that the last element becomes the first one, the second but last one becomes the last one, and so on. `rrr` reverse-rotates both stacks at the same time. |
---
### 🔷 Using a push_swap visualizer
1. I can't recommend this enough.
2. It was very useful for me to see what my code was doing during its implementation, and help with a lot of my debugging.
3. The one I used can be found here https://github.com/o-reo/push_swap_visualizerMake sure you follow this sequence:
1. git clone the repository inside your main push_swap directory, where your push_swap executable will be.
2. Install the required packages as stated on the README.md (do `sudo apt update` first to make sure you have the latest information about available packages)
3. Then, to install a package, do e.g. `sudo apt install cmake`
4. cd inside `/push_swap_visualizer`
5. `mkdir build`
6. cd into `build` then:
- `cmake ..`
- `make`
- Like myself, you might run into some build errors in your terminal. For example, you're missing a OpenGL package. I just chat gpt'd all the error messages and followed the installation commands 😅
7. After a sucessfull build of `cmake ..` and `make`:
- run `./bin/visualizer` and a window of the program should apear.
- change the "push_swap file path" to `../../push_swap`---
### 🔷 Using the checker provided by 42
1. Download the correct file from the subject page, e.g. for Mac, or Linux, inside the same directory as your executable.
2. Running the checker likely won't work, as it won't have the executable permission. Check by typing in the terminal `ls -l`
3. To give it permission, do `chmod +x `
4. Test your executable against everything we need our push_swap to do:
- e.g. the correct outputs for all error types
- e.g. run `ARG="4 10 1 3 2"; ./push_swap $ARG | ./checker_Mac $ARG `
- To see how many instructions, run `ARG="4 10 1 3 2"; ./push_swap $ARG | wc -l`
- For our program to pass the evaluation, it'll have to return `n` size of instructions for sorting `x` number of values:
- If x = 3 then n <= 3
- If x = 5 then n <= 12
- If x = 100 then n < 1500
- If x = 500 then n < 11500
- Note: the lesser instructions our algorithm returns, the more evaluation points we will get.---
### 🔷 Running this code
First off clone this repo. Then compile the program using `make`, and finally pass in a space-separated unordered list of numbers to the program `push_swap`. It will output all the stack operations that it was able to apply sequentially in order to have your list sorted in ascending order.
```bash
git clone https://github.com/RogerioLS/Push_Swap-42sp
cd Push_Swap-42sp
make
./bin/push_swap 42 -42 1 2 3 0 472834 2347 -66573567
```
Obs.: Numbers can range from -2147483648 to 2147483647. Duplicates are not allowed! Try it on and see what happens :)---
### What is an algorithm?
- A set of intructions to solve a problem.### What is the concept of complexity?
- Algorithm analysis:
Analyzing the algorithm's step by step instructions to understand their performance.
- Algorithm efficiency:
Looking at how quickly an algorithm solves a problem, and the resources it uses up, like time and memory.
- Asymptotic Notation:
Using mathematical notations like Big O, Omega and Theta to look at the algorithm's running time, as the problem becomes larger.
- Time complexity:
Using Big O, looking at the best, worst, and average case for an algorithm to complete.
- Space complexity:
Using Big 0, looking at the amount of memory space an algorithm uses.---
> [!NOTE]
> Because of 42 School norm requirements:
> * Each function can't have more than 25 lines of code.
> * All variables are declared and aligned at the top of each function.
> * Project should be created just with allowed functions otherwise it's cheating.