push_swap disc

push_swap

C project · sorting, optimization

Highly optimized sorting with two stacks and a tiny set of allowed operations.

Open it in the console View the code on GitHub Run it in the browser Resume (PDF) LinkedIn

The problem

Sort a stack of integers using a second stack and only 11 operations (swap, push, rotate, reverse rotate), in as few operations as possible.

What I built

The hard part

Counting what each candidate move really costs. Bringing a number to the top of a and its target to the top of b normally costs both rotations added up, but when both sit in the same half of their stack, rr or rrr do them together and the cost is only the larger one. Scoring every number in a this way before each push keeps 500 numbers at 5118 operations on average, 5413 at worst over 300 random runs, under the 5500 limit.

Built with

More projects