EN: push_swap is a sorting project from 42 School. The goal is to sort a stack of integers using a limited set of operations and output the smallest possible number of moves.
ES: push_swap es un proyecto de ordenación de 42. El objetivo es ordenar una pila de enteros usando un conjunto limitado de operaciones e imprimir el menor número posible de movimientos.
- Requirements / Requisitos
- Build / Compilación
- Usage / Uso
- Operations / Operaciones
- Algorithm / Algoritmo
- Input validation / Validación de entrada
- Project structure / Estructura del proyecto
EN
- A Unix-like environment (Linux/macOS)
cccompilermake
ES
- Entorno tipo Unix (Linux/macOS)
- Compilador
cc make
EN / ES
makeThis will build:
push_swap(main program)
To clean:
make cleanTo full clean (including binary):
make fcleanRebuild:
make reEN
push_swap prints to stdout the sequence of operations needed to sort the numbers.
ES
push_swap imprime por stdout la secuencia de operaciones necesarias para ordenar los números.
./push_swap 2 1 3
./push_swap "2 1 3"./push_swap 2 1 3 | wc -lEN
This repository includes a checker_linux binary you can use to validate the output:
ARG="4 67 3 87 23"; ./push_swap $ARG | ./checker_linux $ARGIt should print OK if the operations correctly sort the stack, otherwise KO.
ES
Este repositorio incluye el binario checker_linux para validar la salida:
ARG="4 67 3 87 23"; ./push_swap $ARG | ./checker_linux $ARGDebería imprimir OK si las operaciones ordenan correctamente la pila, si no KO.
EN
The program uses the standard push_swap operations:
sa,sb,ss— swap top two elementspa,pb— push top element between stacks A and Bra,rb,rr— rotate (top becomes bottom)rra,rrb,rrr— reverse rotate (bottom becomes top)
ES
El programa usa las operaciones estándar de push_swap:
sa,sb,ss— intercambia los dos primerospa,pb— empuja el elemento superior entre pilas A y Bra,rb,rr— rota (arriba pasa abajo)rra,rrb,rrr— rota inverso (abajo pasa arriba)
EN This implementation chooses different strategies depending on the input size:
- Small cases: dedicated sorts for 2, 3 and 5 elements
- Larger cases: chunk-based sorting (sending ranges to stack B and rebuilding stack A)
ES Esta implementación elige estrategias según el tamaño:
- Casos pequeños: ordenaciones específicas para 2, 3 y 5 elementos
- Casos grandes: ordenación por “chunks” (enviando rangos a la pila B y reconstruyendo la pila A)
Note / Nota: The exact performance depends on the chunk size and input distribution.
EN The program validates:
- Only integers are accepted
- No duplicates
- Values must fit in 32-bit signed integer range (
INT_MINtoINT_MAX) - On error, it prints
Errortostderr
ES El programa valida:
- Solo acepta enteros
- No permite duplicados
- Los valores deben estar en rango de
int(32 bits con signo,INT_MINaINT_MAX) - En error, imprime
Errorporstderr
EN / ES (overview)
src/— main source code (parsing, operations, sorting)inc/— headers (push_swap.h)libft/— 42 libftft_printf/— custom printfchecker_linux— checker binary for Linux
brturcio