An open API service indexing awesome lists of open source software.

https://github.com/tjensen42/42-push_swap

push_swap will sort random integers on a stack, with a limited set of instructions, trying to use the least stack operations to get a sorted stack.
https://github.com/tjensen42/42-push_swap

42project c c-sorting-algotihms stack-sorting

Last synced: 3 months ago
JSON representation

push_swap will sort random integers on a stack, with a limited set of instructions, trying to use the least stack operations to get a sorted stack.

Awesome Lists containing this project

README

          

# 42-push_swap

The program push_swap will sort random integers on a stack, with a limited set of instructions, trying to use the least stack operations to get an ascending sorted stack.

## How to use it
0. Clone the repo
1. Call ```make release```
3. Start program with your own input: ```./push_swap [put your args seperated by spaces]```
4. Start program with 500 random ints: ```./push_swap `ruby -e "puts (0..499).to_a.shuffle.join(' ')"` ```

## "Game Rules"

The game is composed of 2 stacks named A and B.

Starting Point:
- Stack_A contains random numbers of either positive or negative numbers without any duplicates.
- Stack_B is empty

The goal is to sort in ascending order numbers into stack.

The operations the program can do, are limited to the following set:

| Operations | Description |
| ------------- | --------------------------------------------------------------------------------------|
| sa | swap a - swap the first 2 elements at the top of stack a. Do nothing if there is only one or no elements) |
| sb | swap b - swap the first 2 elements at the top of stack b. Do nothing if there is only one or no elements) |
| 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. Do nothing if b is empty |
| pb | push b - take the first element at the top of a and put it at the top of b. Do nothing if a is empty |
| ra | rotate a - shift up all elements of stack a by 1. The first element becomes the last one |
| rb | rotate b - shift up all elements of stack b by 1. The first element becomes the last one |
| rr | ra and rb at the same time |
| rra | reverse rotate a - shift down all elements of stack a by 1. The last element becomes the first one |
| rrb | reverse rotate b - shift down all elements of stack b by 1. The last element becomes the first one |
| rrr | rra and rrb at the same time |


### Sample sorting animation for 100 integers:

*Visualized with the great push_swap [visualizer](https://github.com/o-reo/push_swap_visualizer) from [o-reo](https://github.com/o-reo). I really appreciate this visualiser, it helped a lot to find strange behaviour of the sorting algorithms.

![push_swap-animation](https://user-images.githubusercontent.com/56789534/137463054-e642a7d5-711d-4f25-bf55-03b47d8fcfc3.gif)



*All 42 projects must be written in C (later C++) in accordance to the 42 School Norm.


> #### Sample restrictions:
> - All variables have to be declared and aligned at the top of each function
> - Each function can not have more then 25 lines
> - Projects should be created with allowed std functions otherwise it is cheating