Skip to content

Sort data using two stacks, using the lowest possible number of actions.

License

Notifications You must be signed in to change notification settings

ThomasRobertson/42-push_swap

Repository files navigation

push_swap

Because Swap_push isn’t as natural.

Line of Code Size License License Norminette Action Norminette Action

SummaryHow It WorkTargetBenchmarksHow To UseHow To Use - Bonus PartLicense

Summary

This project sort data using two stacks, using the lowest possible number of actions.

This project is part of 42 Paris' curriculum. You can check the full subject in the subject pdf included in this repo.

How It Work

To achieve that, the algorithm will first move every number to the second stack, called stack_b, except for three number still in the first stack, called stack_a.

After sorting the remaining three numbers, it will calculate the cost for each number in stack_b to be added, and sorted, into the stack_a. It will always choose the number with the lowest cost. It will repeat this process until every number is sorted.

Target

This program aim at 100% completion and has to sort different size of stack with a maximum number of actions :

  • 3 values : no more than 3 actions
  • 5 values : no more than 12 actions
  • 100 values : no more than 700 actions
  • 500 values : no more than 5 500 actions

Benchmarks

The program was benchmarked using the mean of 100 runs. The range is shown in parentheses.

  • 3 values : mean of 1 (0-2)
  • 5 values : mean of 8 (6-10)
  • 100 values : mean of 561 (540-586)
  • 500 values : mean of 4543 (4384-4819)

Bonus :

  • 1000 values : mean of 11559
  • 2000 values : mean of 29536

How To Use

Clone this GitHub repos

git clone https://github.com/ThomasRobertson/42-push_swap

Use the provided Makefile to compile to program (using the system's cc)

make

Run the program and provided it with a list of integers

./push_swap 7302 3 18 -928 12 0

Or this syntax

./push_swap "7302 3 18 -928 12 0"

Note for 42 Students : the second way is not required by the subject. It is a misconception caused by using zsh instead of bash. See Slack or Discord for more infos.

How To Use - Bonus Part

The Makefile include a checker program that can be compiled with

make bonus

And then run with either

./checker 7302 3 18 -928 12 0

Or

./checker "7302 3 18 -928 12 0"

The program will then wait for inputs. Each action has to be followed by a new line. Once every action has been submitted, use Ctrl+D.

License

MIT