Skip to content

πŸ“š sorting algorithm program using two stacks and a limited set of instructions.

Notifications You must be signed in to change notification settings

okbrandon/push_swap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

80 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“š push_swap

Push_swap is a 42 school project that consists in creating a program that sorts a stack of integers with the help of a second stack, using a limited set of instructions.

Top used language Norminette run GitHub last commit

πŸ“š Table of Contents

πŸ‘¨β€πŸ« Demonstration

Here's how the algorithm works with 500 random values.

Screen.Recording.2023-03-06.at.5.01.39.PM.mp4

The visualizer used: https://github.com/o-reo/push_swap_visualizer

πŸ“¦ Installation

Clone the repository from GitHub:

git clone https://github.com/okbrandon/push_swap.git

Compile the push_swap executable:

make

Compile the checker executable:

make bonus

πŸ“ Usage

Run the sorting program:

./push_swap [numbers]

The first number of your input is at the top of the stack by default. The program will print the instructions to sort the stack.

Run the checker program:

./checker [numbers]

The program will wait for you to enter instructions to sort the stack. You can also pipe the instructions of the sorting program to the checker program like below. When you're done entering instructions, don't just press enter, type CTRL + D to end the program.

πŸ“ Example

Sorting algorithm:

$> ./push_swap 8 9 2 3 1
rra
pb
ra
ra
pb
pa
pa

Checker program:

$> ARG="8 9 2 3 1"; ./push_swap $ARG | ./checker $ARG
OK

The checker returns OK when the stack is sorted. It'll return KO if the stack isn't sorted or if the second stack's not empty.

πŸ“Ž Algorithm

The algorithm used to sort a stack of 100 and 500 numbers is based on the Quick Sort algorithm. It's a divide and conquer algorithm that sorts a stack by splitting it into smaller stacks, sorting them and then merging them back together. Here you can find the article that explains how the algorithm works and how to implement it.

Implementing this algorithm won't get you the highest grade (aka. 100/100) because you have to optimize it and I'll explain how I managed to do it. The goal here is to sort 100 numbers in less than 700 moves and 500 numbers in less than 5500 moves.

First optimization

To do so, when pushing a number from the stack A to the stack B, I check if the number is smaller than the median of its chunk. If it is, I push it to the stack B (pb) then I rotate the stack B (rb) to put the number at the bottom of the stack. If it's not, I simply push it to the stack B (pb). When the stack A is empty, I then search for the two biggest numbers in the stack B and push them to the stack A (pa) depending on their position (index). If they aren't pushed in the right order, I simply swap them (sa). Finally, I repeat the process until the stack B is empty.

Second optimization

When printing the instructions to sort the stack, sometimes you can find a sequence of instructions that can be replaced by a single instruction. For example, if you have to rotate the stack A (ra) and then rotate the stack B (rb), you can simply print one instruction (rr). To do so, I created a function that makes the instructions asynchronic. It means that if you call an instruction, it will wait for the next instruction to be called before printing it. If the next instruction and the previous one can be replaced by a single instruction, it will print it instead of the two instructions. If not, it will simply print the previous instruction and the current one. You can find this function in the srcs/ft_instructions_printer.c file.

⬆ Back to Top

🌏 Meta

bsoubaig – bsoubaig@student.42nice.fr

About

πŸ“š sorting algorithm program using two stacks and a limited set of instructions.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published