Back to projects

42 project

Push Swap

An algorithmic project about sorting data with a very limited set of operations.

Stack

  • C
  • Algorithms
  • Sorting

Skills gained

  • Working with stack-based data structures
  • Designing a sorting strategy under strong constraints
  • Optimizing the number of generated operations
  • Making an algorithm more readable, testable and robust

Links

Push Swap is an algorithmic project where the goal is to sort a stack of integers using a limited set of operations. The challenge is not only to produce a correct result, but to design an efficient, readable and optimized strategy in terms of generated moves.

01 — Constraints

Sorting with tied hands.

The project asks you to sort numbers using only a few allowed operations on two stacks. This constraint forces a different way of thinking: every move matters, and a naïve solution quickly becomes too expensive.

Illustration of a sorting algorithm manipulating two stacks
Fig. 01

02 — Strategy

Choosing the right path.

The main challenge is to build an approach that remains efficient across different input sizes. This means comparing multiple strategies, measuring the results and iterating until finding the right balance between simplicity and performance.

Illustration of an optimization strategy for a sorting algorithm
Fig. 02
One operation less, and somehow the day gets better.

03 — Optimization

Counting every move.

Push Swap teaches you to look at an algorithm not only as an idea, but as a concrete sequence of actions to optimize. It helped me better understand the trade-offs between complexity, readability and real-world performance.

Illustration of debugging a sorting algorithm
Fig. 03
The kind of project where you end up dreaming about stack A and stack B.