https://github.com/clementvidon/pushswap
Sort problem
https://github.com/clementvidon/pushswap
42 42born2code 42cursus 42paris 42projects 42school commoncore pushswap sorting-algorithms sorting-algorithms-implemented
Last synced: 4 months ago
JSON representation
Sort problem
- Host: GitHub
- URL: https://github.com/clementvidon/pushswap
- Owner: clementvidon
- Created: 2022-04-19T14:24:31.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-08-18T15:21:46.000Z (about 3 years ago)
- Last Synced: 2025-03-01T14:18:38.644Z (9 months ago)
- Topics: 42, 42born2code, 42cursus, 42paris, 42projects, 42school, commoncore, pushswap, sorting-algorithms, sorting-algorithms-implemented
- Language: C
- Homepage: https://clemedon.github.io/pushswap_42/
- Size: 1.28 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PUSHSWAP 42
The goal of this project is to sort in ascending order numbers with as few operations as possible.
> cvidon@student.42.fr
> athirion@student.42.fr
> yobougre@student.42.fr
## Index
**[Usage](#usage)**
**[Step 1. Init](#Init)**
**[Step 2. Pre-sort](#pre-sort)**
**[Step 3. Sort](#sort)**
## Usage
Run `make` in the root of the projet and launch as follows:
./push_swap
Example: `./push_swap "5 3 9 2 7"`
***Makefile rules***
- `make` -- compiles philo.
- `make clean` -- deletes object files.
- `make fclean` -- deletes object files and philo.
- `make re` -- `fclean` + `make``.`
## Init
Input: `./push_swap "5 3 9 2 7"`
Init the Stack_A.
Stack_A
5
3
9
2
7
## Pre-sort
First we need to find the `median` so we sort a tab of integer, we
pick its middle element as the `median`.
Tab
9
7
5 ← median
3
2
We push from Stack_A to Stack_B all the elements that are smaller
than the median (by repeating `ra` and using `pb` when required).
Stack_A Stack_B
7 2
9 3
5
We repeat the two previous steps until there are 2 elements left in
`Stack_A`.
Tab
7
9 ← new median
5
Stack_A Stack_B
7 5
9 2
3
## Sort
Now the `pre-sort` is done. We need to find the `leader` and the
`cost` for each element, `pa` the one that has the cheapest cost and
repeat this process.
> The `cost` is the distance to perform to put a `Stack_B` Element in the right
> location of the `Stack_A`.
> The `leader` is the closest value (in terms of value, not distance) between an
> Element in one Stack and an Element in the other Stack.
Stack_A Stack_B |cost|leader|
7 ← leader 5 | 1 | 7 |
9 3 | 2 | 7 |
2 | 2 | 7 |
Move cheapest cost element to `Stack_A`.
Stack_A Stack_B |cost|leader|
5 ← leader 3 | 1 | 5 |
7 2 | 2 | 5 |
9
Move cheapest cost element to `Stack_A`.
Stack_A Stack_B |cost|leader|
3 ← leader 2 | 1 | 3 |
5
7
9
Move cheapest cost element to `Stack_A`.
Sort done
Stack_A
2
3
5
7
9