GithubHelp home page GithubHelp logo

okbrandon / push_swap Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 121 KB

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

Makefile 6.48% C 93.52%
42cursus 42projects 42school pushswap pushswap-42

push_swap's Introduction

๐Ÿ“š 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 โ€“ [email protected]

push_swap's People

Contributors

brandonlooksdumb avatar okbrandon avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.