izzypt / Push_swap

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Push_swap

First, we start with two stacks called A and B.

A is filled with some random integers (without duplicate) and B is empty.

We can perform certain instructions on these stacks, and the goal is to sort all these integers with the least instructions possible.

image

And here is the list of instructions that we can perform :

  • sa (swap a): Swap the first 2 elements at the top of stack a.
  • sb (swap b): Swap the first 2 elements at the top of stack b.
  • ss : sa and sb at the same time.

image

  • pa (push a): Take the first element at the top of b and put it at the top of a.
  • pb (push b): Take the first element at the top of a and put it at the top of b.

image

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

image

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

image

Limit of actions

  • With 3 numbers, we need to sort it with not more than 3 instructions.

  • With 5 numbers, we need to sort it with not more than 12 instructions.

  • With 100 numbers, we can get

    • 5 points if the size of the list of instructions is less than 700

    • 4 points if the size of the list of instructions is less than 900

    • 3 points if the size of the list of instructions is less than 1100

    • 2 points if the size of the list of instructions is less than 1300

    • 1 points if the size of the list of instructions is less than 1500

  • With 500 numbers, we can get

    • 5 points if the size of the list of instructions is less than 5500

    • 4 points if the size of the list of instructions is less than 7000

    • 3 points if the size of the list of instructions is less than 8500

    • 2 points if the size of the list of instructions is less than 10000

    • 1 points if the size of the list of instructions is less than 11500

Implementing the stack

image

Decided to implement the stack by using a linked list.

image

Used the following tester:

Another helpfull topics :

https://medium.com/@jamierobertdawson/push-swap-the-least-amount-of-moves-with-two-stacks-d1e76a71789a

https://medium.com/nerd-for-tech/push-swap-tutorial-fa746e6aba1e

https://medium.com/@ayogun/push-swap-c1f5d2d41e97

https://zainab-dnaya.medium.com/fastest-push-swap-algorithm-2f510028602b

About

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


Languages

Language:C 98.7%Language:Makefile 0.6%Language:Shell 0.6%