Project Overview
Push Swap is a sorting algorithm implementation that sorts a stack of integers using a limited set of operations. The project is designed to meet 42 School standards and follows strict coding conventions.Project Structure
The codebase is organized into functional modules:Core Files
Key Data Structure
The stack is implemented as a doubly-linked circular list:push_swap.h:21-31
Coding Standards
42 Norm Compliance
This project follows the 42 School norm:Naming Conventions
- Functions: lowercase with underscores (
ft_push,sort_three) - Variables: lowercase with underscores (
stack_a,push_cost) - Structures: prefix with
s_(s_list) - Typedefs: prefix with
t_(t_list)
Forbidden Functions
Only the following standard library functions are allowed:
writemallocfreeexit
printf, strlen, etc.) must be implemented manually or avoided.Algorithm Architecture
Sorting Strategy
The algorithm uses different strategies based on stack size:push_swap.c:59-70
Turk Algorithm (Large Stacks)
For stacks > 3 elements, the algorithm:- Push to B: Push all but 3 elements to stack B, choosing optimal targets
- Sort remaining 3: Use
sort_three()on stack A - Push back to A: Move elements from B to A in optimal order
- Final rotation: Rotate stack A to position minimum at top
sort_big_stacks.c:53-75 for implementation details.
Areas for Contribution
1. Performance Optimization
Current performance (approximate):- 100 elements: ~700 operations
- 500 elements: ~5500 operations
- Improve target node selection in
init_a_to_b.c - Optimize cost calculation in
init_nodes_a() - Experiment with different initial push strategies
- Fine-tune the threshold for using
sort_three()
2. Code Refactoring
Potential improvements:- Extract common patterns in operation functions
- Reduce code duplication between
init_a_to_b.candinit_b_to_a.c - Improve error message clarity
- Add more comprehensive input validation
3. Documentation
Documentation needs:- Add inline comments explaining complex algorithms
- Document the cost calculation formula
- Create visual diagrams of the sorting process
- Add more usage examples
4. Testing Infrastructure
Testing improvements:- Create automated test suite
- Add benchmark comparison tools
- Implement a checker program (bonus)
- Add fuzzing for edge cases
- Create visual debugging tools
5. Additional Features
Bonus implementations:- Checker program: Validates operation sequences
- Visualizer: GUI to watch sorting in real-time
- Performance analyzer: Detailed operation statistics
- Alternative algorithms: Implement other sorting approaches
Development Workflow
Setting Up
Making Changes
Make your changes
- Follow the 42 norm strictly
- Keep functions under 25 lines
- Test thoroughly
- Check for memory leaks
Code Review Checklist
Before submitting:- Code follows 42 norm (use
norminetteif available) - All functions are under 25 lines
- No lines exceed 80 characters
- No forbidden functions used
- No memory leaks (Valgrind clean)
- All error cases handled properly
- Code compiles with
-Wall -Wextra -Werror - Tests pass for all stack sizes
- Performance meets benchmarks
Common Pitfalls
Memory Management
Edge Cases
Integer Overflow
Debugging Tips
Enable Debug Mode
Add a debug flag to print operations:Track Operation Count
Resources
Documentation
- 42 Norm - Coding standard
- Push Swap Subject - Project requirements
- Algorithm Guide - Detailed algorithm explanation
Tools
- norminette: Norm checker for 42 projects
- valgrind: Memory leak detection
- gdb: Debugger for C programs
- push_swap_visualizer: Visual debugging tool
Community
- 42 Network: Connect with other students
- GitHub Issues: Report bugs and request features
- Pull Requests: Submit your improvements
Getting Help
If you need assistance:- Check existing documentation and tests
- Review similar issues on GitHub
- Ask in the community forums
- Create a detailed issue report
License and Attribution
When contributing:- Maintain the existing header format in all files
- Credit original author (iboiraza)
- Add your contributions to a contributors list if appropriate
- Respect 42 School academic integrity policies
This is an educational project. If you’re a 42 student, ensure your contributions comply with your school’s collaboration policies.
Next Steps
- Learn the build process
- Set up your testing environment
- Study the algorithm implementation
- Start with small improvements and grow from there!