Giter Site home page Giter Site logo

42_push_swap's Introduction

42_push_swap

Push Swap is a program to sort a list of numbers with two stacks but with a limited set of instructions, in as few moves as possible.

The set of instructions

To manipulate the stack "a" and the stack "b", 11 operations are possibles:

  • 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.

Example

This example sorts the integers of stack "a" with 12 instructions, but some optimizations are still possible to do better.
Screenshot 2022-11-22 at 18-34-32 push_swap fr pdf

Expected Results

Two programs are expected in this project.
The first one is the "push_swap" program that takes the list of numbers as input, determines the best use of instructions to sort the list then outputs them in the terminal.
Screenshot 2022-11-25 at 14-42-26 push_swap fr pdf

The second one is the "checker" program that takes the same list of numbers as input, reads and applies the instructions received by the terminal then finally confirms that the list is correctly sorted (OK) or not... (KO).
Screenshot 2022-11-25 at 14-44-05 push_swap fr pdf

Both programs can be combined to check automatically the result.
Screenshot 2022-11-25 at 14-45-13 push_swap fr pdf

Install

Create or update both programs ("push_swap" and "checker") from sources.

make

Reinstall

Recompile completely both programs.

make re

Clean

Delete object files created during installation.

make clean

Uninstall

Delete object files but also both compiled programs.
It will not affect the source code.

make fclean

Usage

./push_swap 125 -1 65535 -42 [...]

./checker 125 -1 65535 -42 [...] -v -c -h [...]
 
./checker -v -c -h [...] 125 -1 65535 -42 [...]

./checker [...] 125 -v -1 65535 -c [...] -h -42 [...]

ARG="[...] 125 -1 65535 -42 [...] -v -c -d [...]"; ./push_swap $ARG | ./checker $ARG

ARG="`ruby -e "puts (0...500).to_a.shuffle.join(' ')"` -c -n -d [...]"; ./push_swap $ARG | ./checker $ARG

MAN (complete user manual)

./checker -h

Screenshot

Capture d’écran du 2022-11-25 15-34-57

Notes

In addition to the many searchs and retries to find the best algorithm, another difficult part of this project is to adapt each sorting concept using only the limited set of instructions.
To reach the highest level of optimization required (in term of number of instructions used), I had to combine two sorting algorithms: Quicksort and Insertsort.

Keywords

Manipulation of stack
Sorting algorithms
Algorithms Complexity (Big O Notation)

42_push_swap's People

Contributors

fdgbt 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.