Git Product home page Git Product logo

push_swap_42school's Introduction

Push_swap_42school

The project push_swap that sorts integers given as arguments with the help of two stacks and limited number of operations (pdf file attached).

Thanks to kmills (https://github.com/soldatsolt) for explaining the algorithm and helping in the beginning. The project was done only for the level in School 21.

Usage

You need to compile the program and then give the program integers that you want to sort.

So, your steps -

Clone the repository:

git clone https://github.com/VladlenaSkubi-du/Push_swap_42school.git [folder's name you want]

Get into it and compile the checker and push-swap-programs:

cd [folder's name you want]
git submodule add https://github.com/VladlenaSkubi-du/libft_42school.git libft
make

CHECKER PART

After you make:

./checker [numbers you want]

...and then input 2 or 3 letters for operations to be done. To finish the input, you send ctrl + D signal (EOF in Unix, ^D on Mac OS)

For example:

./checker 5 10 2 8 4 15

and then:

pb
pb
pb
sa
pa
rra
pa
rra
pa
rra
rra
^D

Checker can give you answers: "OK" if such operations sorts the stack, "KO" if not and "Error" if the following is true:

  • there are not integers in the array input;
  • there are dublications in the array input;
  • "-5-", "--4", "+6+" and other mistakes with signs;

PUSH SWAP PART

Or you make:

./push_swap 5 10 2 8 4 15

And get output:

pb
pb
pb
sa
pa
rra
pa
rra
pa
rra
rra

Push swap answers you what operations have to be performed to sort the stack. Can also give you "Error" in the same situations as checker.

Go and try. Good luck!

Algorithm

The beginning of the algorithm and the main points of the subject are explained here: https://medium.com/@jamierobertdawson/push-swap-the-least-amount-of-moves-with-two-stacks-d1e76a71789a.

The algorithm is based on rotations with the following steps:

  1. push all the numbers from input to the a-stack
  2. leave only three numbers in the a-stack, sort them and everything else push to the b-stack
  3. chose the best b-stack-number to move to the a-stack-number and do that using only rotations
  4. move according to the chosen parameters and than repeat untill b-stack is empty

Grade

98/100:

  • 10 numbers are sorted in less than 12 operations
  • 100 numbers are sorted in less than 700 operations
  • 500 numbers are sorted in less than 7000 operations (the best grade for "less than 5500 operations")

push_swap_42school's People

Contributors

vladlenaskubi-du 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.