push_swap
Eleven operations, and not one too many
Sorting a stack of integers with eleven operations only, and in as few moves as possible: the move count is what gets graded. Values are replaced by their ranks, the small cases are written by hand, and the general sort pushes chosen chunks to the other stack before bringing everything back.
- Area
- 42
- algorithms
- sorting
- Stack
- C, Algorithms
- Grade
- 125%
- Date
- 2022
- Links
Eleven operations
Two stacks. A holds the shuffled numbers, B is empty. Eleven operations only: swap the top two of one stack or of both, push the top of one stack onto the other, rotate a stack by one in either direction. The program prints nothing but the sequence of operations that leaves A sorted.
Sorting is therefore not the problem: any method gets there. The problem is the number of moves, because that is what gets graded. A hundred numbers in under seven hundred operations, five hundred in under five thousand five hundred: that is the bar.
Working on ranks
The first simplification happens once, while reading the arguments: every number is given its rank, from one to the size of the stack. The real values are never used again, and the rest of the algorithm reasons on contiguous integers.
That is what makes it possible to talk about “where this number belongs” without ever comparing values that could sit anywhere between the two integer bounds. Reading the arguments also does the thankless work: rejecting what is not a number, what overflows an integer, and duplicates.
The small cases
Two numbers sort with one swap, and that is all. Three sort through a hand-written sequence: bring the largest to the bottom with a rotation, in whichever direction is shorter, then swap the top two if they are out of order. Three operations at worst.
This case is not a convenience, it is the heart of the general sort: the big method is precisely about reducing stack A to three elements.
Emptying half of A
For a larger stack, the first phase pushes numbers into B. Not all of them, and not blindly: A is walked through, pushing those whose rank falls in the lower half and rotating the rest, until only three elements are left in A. Those three go through the three-element case.
Spreading them out like this rather than pushing everything in one block has a direct consequence: B then holds a mix of low and high ranks rather than one homogeneous block, and the insertions to come more often find a place near the top.
The cheapest insertion
This is where the moves are won. Every element of B has a destination in A, which is the smallest rank above its own; if there is none, it goes above the smallest element of A, the stack being circular.
The cost of an insertion is then computed on both sides. How many rotations to bring that element to the top of B, how many to bring its destination to the top of A, counted in the shorter direction, and therefore signed. A negative cost means rotating the other way is cheaper.
The program evaluates that cost for every element of B, picks the cheapest, and carries it out. And when both costs go the same way, it uses the operations that rotate both stacks at once: two rotations paid for once. The element is pushed across, and the whole thing starts again with B one element shorter.

What it costs
Over two hundred random shuffles, the real count sits in a narrow window. A hundred numbers: between 526 and 671 operations, 595 at the median. Five hundred numbers: between 4,449 and 5,265, 4,773 at the median.
The two grading bars are at 700 and 5,500. The worst of the two hundred cases stays under the bar, and that is what matters: it is not the average that gets graded, it is the one shuffle the examiner will draw.
The move count is not the only cost, and the second one is the one the subject never looks at. Three places compare everything with everything: rejecting duplicates, which checks each number read against all the previous ones; assigning the ranks, which looks for the largest one left as many times as there are numbers; and above all the main loop, which recomputes the destination of every element of B by walking the whole of A, at every single insertion.
Running time follows. Five hundred numbers take about a second, five thousand more than two minutes. At the graded size it does not show, and the subject never goes further: all the optimisation it asks for is about the length of the output, not the time spent producing it. Still, this is a sort of a few thousand operations, worked out by a program that performs hundreds of millions of them.


The checker
The bonus is a second program that reads a sequence of operations from standard input, applies it to the numbers given as arguments, and answers OK if the stack ends up sorted, KO otherwise.
It is what keeps the sort honest. A sequence of operations can be short and wrong, and without the checker nothing tells the two apart. Piping the output of the first program into the second is the only check worth having.