Skip to content

42-school-projects/pushswap_42

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

67 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

push_swap

Language 42 School Status

A sorting algorithm challenge β€” sort a stack of numbers using the minimum possible number of operations with only two stacks and a restricted set of moves.

πŸ‡§πŸ‡· Leia em PortuguΓͺs


Why this project matters

"This project taught me how to think algorithmically under constraints β€” not just 'sort this', but 'sort this with the fewest possible moves, using only these specific operations'. I learned how indexing simplifies comparisons, when to apply small-sort vs. radix-sort strategies, and how to measure and optimize algorithmic complexity. These are exactly the skills tested in technical interviews and applied in high-performance systems."

Algorithm design and complexity analysis are core skills for any software engineering role. This project goes beyond implementing a known algorithm β€” it requires selecting and combining strategies to hit performance targets.


How it works

Two stacks β€” A and B β€” and 11 allowed operations:

Operation Description
sa Swap the top 2 elements of stack A
sb Swap the top 2 elements of stack B
ss Execute sa and sb simultaneously
pa Push top of B to top of A
pb Push top of A to top of B
ra Rotate A β€” top element goes to bottom
rb Rotate B β€” top element goes to bottom
rr Execute ra and rb simultaneously
rra Reverse rotate A β€” bottom element goes to top
rrb Reverse rotate B β€” bottom element goes to top
rrr Execute rra and rrb simultaneously

Goal: Sort stack A in ascending order (smallest on top) with the fewest operations. Stack B must be empty at the end.


The algorithm

1. Input validation

Verifies that all inputs are valid integers within the int range and that there are no duplicates.

2. Indexing

Before sorting, all numbers are replaced by their sorted index (0, 1, 2...). This eliminates the need to handle negative numbers and large values during sorting, simplifying all comparisons.

3. Sorting strategy

Small sort (≀ 5 elements) A hardcoded decision tree optimized for the minimum number of moves in each possible case.

Radix sort (> 5 elements) Numbers are sorted bit by bit using a binary radix sort over their indexes β€” pushing elements to B based on the current bit, then pulling them back. This guarantees O(n log n) complexity with a consistent and predictable number of operations.


A standout technical detail

The use of indexing as a preprocessing step before sorting is an elegant engineering decision. By converting arbitrary integers into a contiguous index sequence, the radix sort can operate on bits directly without worrying about sign bits, large values, or comparison edge cases. This pattern β€” normalizing data before processing β€” is a recurring technique in real-world data pipelines and competitive programming.


Demo

Sorting demo

5 elements sort

100 elements sort


Getting Started

git clone https://github.com/gustavofsousa/pushswap_42.git
cd pushswap_42
make

Running

# Sort a list of numbers
./push_swap 3 1 4 1 5 9 2 6

# Pipe to count operations
./push_swap 3 1 4 1 5 9 2 6 | wc -l

# Use the checker to verify correctness
./push_swap 3 1 4 1 5 9 2 6 | ./checker 3 1 4 1 5 9 2 6

Project structure

pushswap_42/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ push_swap.c    # Entry point + input validation
β”‚   β”œβ”€β”€ sort_small.c   # Optimized sort for ≀5 elements
β”‚   β”œβ”€β”€ sort_big.c     # Radix sort for large inputs
β”‚   β”œβ”€β”€ push.c         # pa / pb operations
β”‚   β”œβ”€β”€ swap.c         # sa / sb / ss operations
β”‚   β”œβ”€β”€ rotate.c       # ra / rb / rr operations
β”‚   β”œβ”€β”€ reverse_rotate.c # rra / rrb / rrr operations
β”‚   β”œβ”€β”€ checker.c      # Validates sorted output
β”‚   └── ft_atol.c      # Integer parsing
β”œβ”€β”€ include/           # Headers
β”œβ”€β”€ libft/             # Personal C library
└── Makefile

References


Skills demonstrated

  • Algorithm design and optimization under constraints
  • Complexity analysis (O(n log n) target)
  • Data indexing and normalization
  • Bitwise operations (binary radix sort)
  • Linked list stack implementation
  • Handling edge cases in input parsing

License

This project was developed as part of the 42 School curriculum.


Made with β˜• at 42 Rio de Janeiro

About

πŸ€”A project with the objective to sort numbers, limited by some type of moviments and using 2 stacks.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors