Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dpetrosy/42-push-swap
42 School Push_Swap - Algorithmic project on C
https://github.com/dpetrosy/42-push-swap
42 42-cursus 42-push-swap 42born2code 42cursus 42projects 42pushswap 42school 42yerevan algorithm c c-programming push-swap push-swap-42 pushswap pushswap-42 pushswap42 sorting sorting-algorithms stack
Last synced: 21 days ago
JSON representation
42 School Push_Swap - Algorithmic project on C
- Host: GitHub
- URL: https://github.com/dpetrosy/42-push-swap
- Owner: dpetrosy
- License: mit
- Created: 2024-03-03T12:44:40.000Z (10 months ago)
- Default Branch: master
- Last Pushed: 2024-03-18T19:33:41.000Z (9 months ago)
- Last Synced: 2024-03-19T14:39:30.730Z (9 months ago)
- Topics: 42, 42-cursus, 42-push-swap, 42born2code, 42cursus, 42projects, 42pushswap, 42school, 42yerevan, algorithm, c, c-programming, push-swap, push-swap-42, pushswap, pushswap-42, pushswap42, sorting, sorting-algorithms, stack
- Language: C
- Homepage:
- Size: 2.89 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Push-Swap Project
42 Yerevan Push-Swap
For further information about 42cursus and its projects, please refer to 42cursus repo.
๐ About Project
ยท
๐ Operations List
ยท
๐งฎ Algorithm
ยท
๐ Bonus Part
ยท
๐จโ๐ป Usage
---
## ๐ About Project
* This project will make you sort data on a stack, with a limited set of operations, using the lowest possible number of actions.
* The Push_swap project is a very simple and highly effective algorithm project: data will need to be sorted.
You have at your disposal a set of int values, 2 stacks and a set of operations to manipulate both stacks.* The goal is to write a program in C called push_swap which calculates and displays on the standard output the operations, that sort the integers received as arguments.
* The learning objectives of this project are rigor, use of C and use of basic algorithms. Especially looking at the complexity of these basic algorithms.
* You are allowed to use the following functions: malloc, free, exit, write, read.
For detailed information, refer to the [**subject of this project**](README_files/push_swap_subject.pdf).
๐ TLDR: Given a set of integers, sort it using a predefined set of operations using
two stacks. The operations list is defined below. This program outputs
push_swap operations that sorts the input integers.> [!NOTE]
> Because of 42 School norm requirements:
> * Each function can't have more then 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.## ๐ Operations List
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 stack B is empty.The possible operations are:
| Code | Operations | Action |
|:-----:|:-----------------------------------:|:------------------------------------------------------:|
| `sa` | swap a | swaps the 2 top elements of stack a |
| `sb` | swap b | swaps the 2 top elements of stack b |
| `ss` | swap a + swap b | both `sa` and `sb` |
| `pa` | push a | moves the top element of stack b at the top of stack a |
| `pb` | push b | moves the top element of stack a at the top of stack b |
| `ra` | rotate a | shifts all elements of stack a from bottom to top |
| `rb` | rotate b | shifts all elements of stack b from bottom to top |
| `rr` | rotate a + rotate b | both `ra` and `rb` |
| `rra` | reverse rotate a | shifts all elements of stack a from top to bottom |
| `rrb` | reverse rotate b | shifts all elements of stack b from top to bottom |
| `rrr` | reverse rotate a + reverse rotate b | both `rra` and `rrb` |## ๐งฎ Algorithm
I use `Butterfly` algorithm for sorting. Here is a short description:
* First, need to generate a chunk number.
* After I iterate through stack A look for a number, whose index is smaller than the counter (numbers count in stack B).
* If I find that number, I do `pb` and `rb`.
* If no, I check if the number is smaller than counter + chunk, and do only `pb`.
* Repeat these operations until stack A becomes empty.
* In stack B we will have numbers in order like 90-degree rotated butterfly ๐ฆ.
* After that, I will iterate through stack B and push the maximum index in stack A (i.e. `pa`) until stack B becomes empty.
* After these operations, stack A becomes sorted.The chunk number must be different depending on the numbers count. \
So need to experiment with chunk number to achieve the best operations count. \
For more information about the algorithm see this video. \
Also, you can visualize algorithm work by this visualizer.## ๐ Bonus Part
* The Bonus exercise was to write a program called 'checker' that can read the operations from stdin and perform them on a stack of numbers.
* Checker will display 'OK' when the given operations will sort the numbers in ascending order. Otherwise it displays 'KO'.
* In case of an error (f.ex. operations doesn't exist) it displays 'Error'.## ๐จโ๐ป Usage
### RequirementsThe program is written in C language and thus needs the **gcc compiler** and some standard **C libraries** to run.
### Instructions
**1. Compiling the program**
To compile the program, run:
```shell
$ cd path/to/push_swap && make
```To compile the checker, run:
```shell
$ cd path/to/push_swap && make bonus
```**2. How to run the program**
Run the executable together with the set of numbers separated by a space:
```shell
./push_swap 5 7 10 4 6 8 9 2 1 3
```To run the checker on push_swap, use the pipe:
```shell
ARG="5 7 10 4 6 8 9 2 1 3"; ./push_swap $ARG | ./checker $ARG
```Use `clean` to delete all object files, `fclean` to remove executable and all object files, and `re` to recompile the program:
```shell
make clean / make fclean / make re
```