I knocked together an almost identical bit of code, apart from the gt bit, which really is the same:
sub sort_stack {
local $depth = pop @stack;
local $sort_depth = $depth;
for (1..$depth) {
for (1..$sort_depth) {
local $top = pop @stack;
local $next = pop @stack;
if ($top gt $next) {
$top ^= $next;
$next ^= $top;
$top ^= $next;
}
push @stack, $next, $top;
rotate_up($sort_depth);
}
rotate_up($sort_depth);
--$sort_depth;
}
push @stack, $depth;
}
Although I hadda go and use an xor swap, to make it look quite cool..