Skip to main content

Quick Start Guide

This guide will get you from zero to sorting stacks with Push Swap in just a few minutes.
1

Clone the Repository

Clone the Push Swap repository to your local machine:
git clone https://github.com/ibon-ira/Push_swap-42-turk-algorithm-.git
cd Push_swap-42-turk-algorithm-
2

Build the Program

Compile the program using the provided Makefile:
make
This compiles all source files and creates the push_swap executable.
3

Run Your First Sort

Sort a simple list of numbers:
./push_swap 3 2 1
Output:
sa
rra
These operations will sort the numbers in ascending order!
4

Verify the Sort

The output shows the sequence of operations needed. Each line represents one stack operation:
  • sa - Swap the top two elements of stack A
  • rra - Reverse rotate stack A (move bottom element to top)
After these operations, stack A contains [1, 2, 3] in sorted order.

Try Different Examples

Two Numbers

./push_swap 5 3
Output:
sa
A single swap is all you need!

Already Sorted

./push_swap 1 2 3 4 5
Output:
(no output - already sorted!)
The program detects when input is already sorted and outputs nothing.

Larger Input

./push_swap 5 2 8 1 9 3
The program will output a sequence of operations to efficiently sort this larger set.

Understanding the Output

Each operation manipulates one or both stacks:

Swap Operations

sa, sb, ss - Swap top two elements

Push Operations

pa, pb - Move element between stacks

Rotate Operations

ra, rb, rr - Shift all elements up

Reverse Rotate

rra, rrb, rrr - Shift all elements down

Common Use Cases

Testing with Random Numbers

Generate random input using shell commands:
./push_swap $(seq 1 100 | shuf | tr '\n' ' ')
This sorts 100 random numbers and shows the operation sequence.

Counting Operations

See how many operations are needed:
./push_swap 5 2 8 1 9 3 | wc -l
The count shows the algorithm’s efficiency for that input.

Input as a String

You can also pass numbers as a single quoted string:
./push_swap "3 2 1"
This is equivalent to ./push_swap 3 2 1.

Next Steps

Learn the Algorithm

Understand how the Turkish algorithm works

Operations Reference

Detailed documentation of all operations

More Examples

See comprehensive usage examples

Error Handling

Learn about input validation

Need Help?

Build docs developers (and LLMs) love