Coder Social home page Coder Social logo

amaitou / push_swap Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 129 KB

Conquer the array sorting puzzle with our project. Navigate the task using a limited set of instructions, sparking creativity in devising efficient sorting strategies for an array of integers. This endeavor offers a unique twist on traditional sorting algorithms, emphasizing concise and effective instruction sets.

Makefile 2.35% C 97.65%
1337school 42cursus 42network c pushswap-42 sorting-algorithm

push_swap's Introduction


70d6f4e10e2badb5ef394f00c17ad2bc1c14f6e7


Push Swap

The Push swap project is a very simple and highly straightforward algorithm project:

data must be sorted.

You have at your disposal a set of integer values, 2 stacks, and a set of instructions to manipulate both stacks. Your goal? Write a program in C called push_swap which calculates and displays on the standard output the smallest program, made of Push swap language instructions, that sorts the integers received as arguments.

Note: I haven't shared the PDF of this project, as well as, I haven't explained anything due to school privacy reasons!

This project consists of two parts:

  • Mandatory part
  • Bonus Part

Note: The Bonus Part is not that necessary to validate the project, but it gives some extra XPs and days for the The Blackhole.


Makefile

Here are the rules that I have used in this project's makefile:

  • all -> to make the mandatory part
  • bonus -> to make the mandatory as well as the bonus part
  • clean -> to clean the project from all executable and object file
  • fclean -> It does the same thing as clean but it removes the compiled libraries as well
  • re -> it deletes and re-compiles all the things from scratch

The Algorithm

Since we have a limited number of instructions as well as a stack to deal with, we have to make that we have to implement an algorithm that efficiently sorts the stack in a small number of instructions.
That's why I have many and many algorithms to have the projects done but they don't make me feel satisfied as some of them are too fucking complicated and others will not give the full score (bonus included) like the sorting algorithm Radix Sorting Algorithm.
that's what led me to find another algorithm that gives the full score as well as it is much easier rather the other ones, and here is how it works.

First of all, let's see the instructions we have to sort the stack :

  • sa (swap a): Swap the first 2 elements at the top of stack a. Do nothing if there is only one or no element.
  • sb (swap b): Swap the first 2 elements at the top of stack b. Do nothing if there is only one or no element.
  • 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.

first of all, after parsing the arguments into the stack a and checking if there is a weird character rather than a number, we should create another stack that has the elements from the origin stack, but this time the temporary stack should have the elements sorted (I have used the bubble sort to sort them).

secondly, hold the top of stack a and get its index from the sorted stack once you have it, let's check if does belong to the range or not.

100 -> The range will be something similar to (0.15)

500 -> The range will be something similar to (0.30)

  • If the index of the top of stack a from the sorted stack is less than or equal to the start of the range, we pb the element to stack b, rotate the stack b and increase the start and the end of the range by one.
  • If the index of the top of stack a from the sorted stack is greater than the start of the range and smaller than the end of the range, we pb the element to stack b and increase the start as well as the end of the range by one.
  • If the index of the top of stack a from the sorted stack is greater than or equal to the end of the range, we ra the top of stack a.

once stack a is empty we have to get back elements from stack b, but this time it would be much easier as we have in stack b the elements cheaped in the form of a mirror pyramid, so all you have to do, is to get the index of the maximum number in stack b and check if the index if in the second half of the stack b then keep rra the element until it reaches the top then push it back to stack a (pa) otherwise (ra)

By the end of this algorithm, you will see this stack sorted in an appropriate number of instructions.


Things you will learn in this project

  • Data Structures (Stacks)
  • Makefile
  • Structs
  • Sorting algorithms
  • Memory leaks
  • Problem-solving

Contact Me


push_swap's People

Contributors

amaitou avatar

Stargazers

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