This project was my first experience working with sorting algorithms. The program takes as arguments a set of unique integers, and sorts them using only two stacks, using only push_swap instructions, as efficiently as possible (in terms of number of moves, not in terms of computational speed).
- 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.
The goal was to fully sort the integers provided, under the following limits:
- 5 integers: 12
- 100 integers: 700
- 500 integers: 5500
I decided that it would be easier to complete this project if I was able to use vectors, so it would be easier to insert and move values than it would be with a standard array. Since vectors are not available by default in C, I followed this very helpful tutorial (https://github.com/juliuskoskela/c-vec) and added the relevant functions to my libft library.
For small sets, the logic is very straightforward and largely hardcoded. For larger sets (>5 integers), we use an insertion sort model that pushes integers into reverse order in stack b, until there are three remaining in a. It then pushes all the b stack back into a in order.
The biggest challenge was choosing the integers with the lowest cost to move to the appropriate spot in stack b, considering that double moves are more economical (rr and rrr operations). My project also discounts the cost of moving one integer if the following integer would also be able to be immediately inserted. This step improved efficiency a great deal.
./push_swap 5 4 6 7 2 1 3
pb
pb
pb
rrb
sb
pb
sa
rra
pa
sa
pa
sa
pa
sa
pa
sa
rra
rra
Here is a visualization of the before and after of this example sort, using the repo here: https://github.com/o-reo/push_swap_visualizer
![Screen Shot 2024-01-30 at 12 35 07](https://private-user-images.githubusercontent.com/47835762/300774749-5e52725d-403f-48ab-a198-7f11a0b94d2e.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQyODUwMDksIm5iZiI6MTcyNDI4NDcwOSwicGF0aCI6Ii80NzgzNTc2Mi8zMDA3NzQ3NDktNWU1MjcyNWQtNDAzZi00OGFiLWExOTgtN2YxMWEwYjk0ZDJlLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIxVDIzNTgyOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTc1MzhlNmM1ODQ3MTViZTBiYmFiNWM2N2Q5MDI4YzkxZTQ0NTEzOWQ5OTkxYjFmMGQ4NTYzOTJiMjE2OGJiNTgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0._1JWu6ZXl-JhGYx53Qt8RKYFrw4xpHV8nL0akCf5b3Y)
![Screen Shot 2024-01-30 at 12 35 26](https://private-user-images.githubusercontent.com/47835762/300774766-175056b7-a155-4bb0-a826-21ca294c0067.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQyODUwMDksIm5iZiI6MTcyNDI4NDcwOSwicGF0aCI6Ii80NzgzNTc2Mi8zMDA3NzQ3NjYtMTc1MDU2YjctYTE1NS00YmIwLWE4MjYtMjFjYTI5NGMwMDY3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODIxVDIzNTgyOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWJjMmM0YmFkMDFmNzQ0MTc4YjZhOGVkZTQ2MzczOWEyNWQxNWVjODQ1NmNmZTdhMTcwODU5OTYwOTE4MDM5M2EmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.D8mlZXotr4DEhA_VlRuydXX8PLXqZ8oZAHcAYAvpwaY)