Skip to content

push_swap will sort random integers on a stack, with a limited set of instructions, trying to use the least stack operations to get a sorted stack.

Notifications You must be signed in to change notification settings

tjensen42/42-push_swap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

42-push_swap

The program push_swap will sort random integers on a stack, with a limited set of instructions, trying to use the least stack operations to get an ascending sorted stack.

How to use it

  1. Clone the repo
  2. Call make release
  3. Start program with your own input: ./push_swap [put your args seperated by spaces]
  4. Start program with 500 random ints: ./push_swap `ruby -e "puts (0..499).to_a.shuffle.join(' ')"`

"Game Rules"

The game is composed of 2 stacks named A and B.

Starting Point:

  • Stack_A contains random numbers of either positive or negative numbers without any duplicates.
  • Stack_B is empty

The goal is to sort in ascending order numbers into stack.

The operations the program can do, are limited to the following set:

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

Sample sorting animation for 100 integers:

*Visualized with the great push_swap visualizer from o-reo. I really appreciate this visualiser, it helped a lot to find strange behaviour of the sorting algorithms.

push_swap-animation


*All 42 projects must be written in C (later C++) in accordance to the 42 School Norm.

Sample restrictions:

  • All variables have to be declared and aligned at the top of each function
  • Each function can not have more then 25 lines
  • Projects should be created with allowed std functions otherwise it is cheating

About

push_swap will sort random integers on a stack, with a limited set of instructions, trying to use the least stack operations to get a sorted stack.

Topics

Resources

Stars

Watchers

Forks