See it in the room

C · algorithms · 42 Common Core

push_swap

Sorting numbers with two stacks and a handful of allowed moves, using as few moves as possible.

The problem

The problem

Sort a list of integers when the only tools are two stacks and a fixed set of operations, and output the shortest sequence you can find.

What I built

  • A program that reads the numbers, checks them and prints a valid sequence of stack operations that leaves stack A sorted.

Technical decisions

  • Correctness first, then the number of operations, which is what the project is graded on.

What makes it hard

  • No random access: every move has to be expressed through the allowed operations, so normal sorting algorithms do not apply directly.

What I can do because of it

  • Reason about the cost of an algorithm and optimise for a measured target.
  • Solve a problem under hard constraints instead of reaching for a library.