pgomez-a / push_swap

This project will make us sort data on a stack, with a limited set of instructions, using the lowest possible number of actions. To succeed we will have to manipulate various types of algorithms and choose the one (of many) most appropriate solution for an optimized data sorting.

Home Page:https://www.linkedin.com/in/pgomez-a/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

push_swap

push_swap

If you want to learn more about IT topics, I invite you to join my Patreon channel and visit my website: IA Notes

How to run push_swap?

  1. Clone push_swap repository:

    git clone https://github.com/pgomez-a/push_swap.git && cd push_swap
    
  2. Run make to create the program:

    make
    
  3. Run push_swap with the list of numbers you want to sort:

    ./push_swap 4 3 2 1
    
  4. Run checker to see if the numbers are ordered correctly:

    ./checker 4 3 2 1
    pb
    ra
    sa
    pa
    ra
    ^D
    

This project is simply a good way to get involved in the world of algorithms and everything related to their optimization. Due to the limitations imposed on us, we have to find, among many algorithms, the best one to sort an unordered list of numbers stored in a stack.
unsorted stack

A brief introduction to algorithms

I am currently working on another repository where I want to explain in more detail what algorithm analysis consists of and what are the different types of algorithms that we can use depending on the objective we want to achieve. All the information you will find here is a combination of the things I studied before starting my push_swap and the things I came across while reading different algorithm books. sorted stack

Instructions

In this project, we have to sort a list of numbers. We have at our disposal two stacks to manipulate the numbers for the sort operation and a set of instructions to manipulate both stacks. With this, we have to write two programs:

  • Checker: takes integer arguments and reads instructions on standard input. Once read, it executes them and shows OK if the integers are sorted or KO if not.
  • Push_swap: calculates and displays on standard output the instructions needed to sort the given stack. Our algorithm should use the fewest number of rules necessary to sort the stack. Our grade will totally depend on this (depending on the optimization topic).

General Rules

We can only use two stacks to manipulate the elements. This means that when it comes to getting information from a stack, we can do whatever we want if we don't manipulate the elements. But when we want to stack, unstack, rotate, etc., we can only use these two stacks and the rules explained below. This is very important because otherwise we could just use a quick sort to sort all the items and that would be the whole project. But since we want to expand our knowledge, we have to make an effort to read the different ways of implementing algorithms and how to use the least amount of resources.
When one of the programs starts, stack a contains a random list of numbers and stack b is empty. After the program ends, stack a must contain all sorted items and stack b must be empty. Remember that the goal is to sort the numbers in ascending order on stack a.

Handling Rules

  • sa: swap a --> swap the first two elements at the top of stack a.
  • sb: swap b --> swap the first two elements at the top of stack b.
  • ss: sa & sb at the same time.
  • pa: push a --> unstack the first element of stack b and put it in stack a.
  • pb: push b --> unstack the first element of stack a and put it in stack b.
  • ra: rotate a --> shift up all elements of stack a by 1.
  • rb: rotate b --> shift up all elements of stack b by 1.
  • rr: ra & rb at the same time.
  • rra: reverse rotate a --> shift down all elements of stack a by 1.
  • rrb: reverse rotate b --> shift down all elements of stack b by 1.
  • rrr: rra & rrb at the same time.

./push_swap

push_swap test push_swap test push_swap test push_swap test

./checker

checker test checker test checker test checker test

./checker | ./push_swap

push_swap & checker test push_swap & checker test

About

This project will make us sort data on a stack, with a limited set of instructions, using the lowest possible number of actions. To succeed we will have to manipulate various types of algorithms and choose the one (of many) most appropriate solution for an optimized data sorting.

https://www.linkedin.com/in/pgomez-a/

License:MIT License


Languages

Language:C 96.6%Language:Makefile 3.4%