say "Insertion Sort LEGEND: ! mark to the left of which the array is sorted | placeholder for above mark while invariant may be temporarily violated > pointer to pair of items that will be swapped if out of order "; my @list = split "", chomp =$IN; sub display (@separators) { say (join ' ', (@separators ¥ @list,' ')); } sub swap (@_ is rw) { @_[0,1] = @_[1,0] } OUTER: for 1..@list.end -> $sorted { display (' ' xx $sorted, '!', ' ' xx (1+@list.end-$sorted)); for reverse (0..$sorted-1) -> $idx { display (' ' xx $idx, '>', ' ' xx ($sorted-$idx-1), '|', ' ' xx (@list.elems-$sorted)); if @list[$idx] gt @list[$idx+1] { swap @list[$idx, $idx+1]; } else { next OUTER; } } display (' ' xx $sorted, '|', ' ' xx (1+@list.end-$sorted)); } display (' ' xx @list.elems, "!");