Skip to main content

Command-Line Syntax

The Push Swap program accepts integers as command-line arguments and outputs the sequence of operations needed to sort them.

Basic Invocation

./push_swap [numbers...]

Input Formats

Push Swap supports two different input formats, handled in main.c:15-35:
./push_swap 3 2 1 5 4

Multiple Arguments Format

When you pass numbers as separate arguments, each number is a distinct command-line parameter:
./push_swap 42 -7 0 15
This format is processed by passing argv + 1 directly to ft_fill_stack() with flag 0 (main.c:31).

Single String Format

When you pass numbers as a single quoted string, the program splits it internally:
./push_swap "42 -7 0 15"
This format triggers ft_split() to parse the string by spaces (main.c:26), then processes the result with flag 1 to ensure proper memory cleanup.
Both input formats produce identical results. The single string format is useful when numbers come from variable expansion or script output.

Understanding the Output

The program outputs a sequence of operations, one per line:
./push_swap 3 2 1
Output
sa
rra
Each line represents one operation that transforms the stack. The operations are:
  • sa, sb, ss: Swap operations
  • pa, pb: Push operations
  • ra, rb, rr: Rotate operations
  • rra, rrb, rrr: Reverse rotate operations
For detailed information about each operation, see the Operations Reference.

Output Interpretation

The output sequence represents the steps to sort the input numbers in ascending order:
  1. All numbers start in stack A
  2. Stack B starts empty
  3. Apply each operation in order
  4. Final result: sorted numbers in stack A, stack B empty

Example Walkthrough

For input 3 2 1:
1

Initial State

Stack A: [3, 2, 1] (top to bottom)Stack B: []
2

After 'sa' (swap A)

Stack A: [2, 3, 1]Stack B: []
3

After 'rra' (reverse rotate A)

Stack A: [1, 2, 3] - Sorted!Stack B: []

Edge Cases

The program handles several edge cases automatically:

Empty or No Input

./push_swap
# No output, exits with status 0

./push_swap ""
# No output, exits with status 0
From main.c:22, the program exits silently when argc == 1 or when given an empty string.

Already Sorted Input

./push_swap 1 2 3 4 5
# No output (no operations needed)
The is_stack_sorted() check in push_swap.c:85 detects pre-sorted input and skips sorting.

Two Numbers

./push_swap 2 1
Output
sa
For exactly 2 numbers, the program uses a single swap operation if needed (push_swap.c:64-65).

Next Steps

Build docs developers (and LLMs) love