https://github.com/azersd/ksort-push_swap
push_swap is a sorting algorithm based on sorting two stacks with the least amount of moves.
https://github.com/azersd/ksort-push_swap
42 42heilbronn 42network algorithms data-structures k-sort ksort push-swap pushswap pushswap-42
Last synced: 4 months ago
JSON representation
push_swap is a sorting algorithm based on sorting two stacks with the least amount of moves.
- Host: GitHub
- URL: https://github.com/azersd/ksort-push_swap
- Owner: AzerSD
- Created: 2023-01-14T08:10:59.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-12-15T20:27:33.000Z (almost 2 years ago)
- Last Synced: 2025-02-15T19:52:50.357Z (9 months ago)
- Topics: 42, 42heilbronn, 42network, algorithms, data-structures, k-sort, ksort, push-swap, pushswap, pushswap-42
- Language: C
- Homepage:
- Size: 48.8 KB
- Stars: 12
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# push_swap

push_swap is a sorting algorithm based on sorting two stacks with the least amount of moves.
The allowed moves are:
* sa (swap a): Swap the first 2 elements at the top of stack a.
* sb (swap b): Swap the first 2 elements at the top of stack b.
* ss : sa and sb at the same time.
* pa (push a): Take the first element at the top of b and put it at the top of a.
* pb (push b): Take the first element at the top of a and put it at the top of b.
* ra (rotate a): Shift up all elements of stack a by 1. First becomes last.
* rb (rotate b): Shift up all elements of stack b by 1. First becomes last.
* rr : ra and rb at the same time.
* rra (reverse rotate a): Shift down all elements of stack a by 1. Last becomes first.
* rrb (reverse rotate b): Shift down all elements of stack b by 1. Last becomes first.
* rrr : rra and rrb at the same time.
>> By using these moves, the goal is to sort the numbers in stack A in ascending order, using stack B as a temporary storage.
>> This algorithm is considered to be one of the simplest sorting algorithm, but it can be quite hard to optimize it and sort a large amount of numbers with a low amount of moves.
# The Algorithm:
https://user-images.githubusercontent.com/56733438/213343838-583f273e-b108-4648-8d9c-8b2b95d1554b.mov
# ksort