Skip to content

This project makes you sort data on a stack, with a limited set of instructions, using the lowest possible number of actions.

Notifications You must be signed in to change notification settings

dspilleb/Push_swap

Repository files navigation

πŸ“– Push_swap | 19 Brussels

GitHub code size in bytes Number of lines of code Code language count GitHub top language GitHub last commit

πŸ’‘ About the project

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

data must be sorted.

🎯 The goal is to sort the stack in an ascending order with the lowest possible number of operations.


πŸ’» To use the program

you must first compile it with the makefile by using the command 'make' in the shell

make all

Then the program must be executed as follows:

./push_swap nb1 nb2 nb3 nbn...

❌ Error handling | Parsing

These lines should produce an Error :

  • arguments aren’t integers.
  • some arguments are bigger/smaller than an integer.
  • there are duplicates.


❔ How it works ?

We have 2 stacks named a and b.

At the beginning stack b is empty and stack a contains the number given as arguments

Here are the operations we can execute :

Operations Description
sa swap first two elements of stack A
sb swap first two elements of stack B
ss sa and sb at the same time
pa pops the first elememt on B and puts it on top of A
pb pops the first elememt on A and puts it on top of B
ra rotates stuck A up by one
rb rotates stuck B up by one
rr rotates both A and B up by one
rra rotates stuck A down by one
rrb rotates stuck B down by one
rrr rotates both A and B down by one

I decided to implement the stack as a double-linked list and I use radix sorting, which works with the binary of the digits.
First I set a rank for each number I have to sort, making the smallest number rank 0, which makes me work only with positive values.
Then I compare each bit and decide whether to move it to stack b or not, repeating this operation until the stack A is sorted.

About

This project makes you sort data on a stack, with a limited set of instructions, using the lowest possible number of actions.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published