push_swap is a sorting algorithm project that sorts a stack of integers using a limited set of operations. The goal is to sort the stack using the minimum number of operations possible, demonstrating algorithm optimization and efficient data structure manipulation.
- Stack Operations: Implements push, swap, and rotate operations on stacks
- Sorting Algorithms: Multiple sorting strategies for different stack sizes
- Optimization: Minimizes the number of operations required
- Visualization: Can display the sorting process step by step
- Performance: Efficient algorithms for various input sizes
sa- Swap the first two elements of stack Asb- Swap the first two elements of stack Bss- Execute sa and sb simultaneouslypa- Push the first element from stack B to stack Apb- Push the first element from stack A to stack Bra- Rotate stack A (first element becomes last)rb- Rotate stack B (first element becomes last)rr- Execute ra and rb simultaneouslyrra- Reverse rotate stack A (last element becomes first)rrb- Reverse rotate stack B (last element becomes first)rrr- Execute rra and rrb simultaneously
push_swap/
├── push_swap.h # Header file with function declarations
├── push_swap.c # Main program logic
├── operations.c # Stack operation implementations
├── sorting.c # Sorting algorithm implementations
├── utils.c # Utility functions
├── Makefile # Build configuration
└── .git/ # Git repository
The project typically uses different sorting strategies based on stack size:
- Small stacks (≤ 3 elements): Simple swap-based sorting
- Medium stacks (4-5 elements): Optimized insertion sort
- Large stacks (> 5 elements): Divide and conquer with multiple stacks
# Compile the program
make
# Sort a list of numbers
./push_swap 2 1 3 6 5 8
# Check if the solution is correct
./push_swap 2 1 3 6 5 8 | ./checker 2 1 3 6 5 8
# Count the number of operations
./push_swap 2 1 3 6 5 8 | wc -l- GCC compiler
- Make utility
- Standard C libraries
- Libft library (dependency)
This project depends on the Libft library for basic string and memory functions.
- 3 numbers: ≤ 3 operations
- 5 numbers: ≤ 12 operations
- 100 numbers: ≤ 700 operations
- 500 numbers: ≤ 5500 operations
- Follows 42 coding style (norminette)
- Implements efficient stack data structures
- Uses optimized sorting algorithms
- Handles edge cases and error conditions
- Memory-safe implementation
shmohamm - 42 Abu Dhabi