Guest User

Untitled

a guest
May 25th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. sub insertion-sort(@list is rw) {
  2. for 1 ..^ @list.elems -> $i {
  3. PRE { [<=] @list[ 0..^$i ] }
  4. my $value = @list[$i];
  5. my $j = $i - 1;
  6. while $j >= 0 && @list[$j] > $value {
  7. PRE { all(@list[$j..$i]) >= $value }
  8. @list[ $j + 1 ] = @list[$j];
  9. --$j;
  10. }
  11. @list[ $j + 1 ] = $value;
  12. }
  13. @list
  14. }
  15.  
  16. say insertion-sort( [ 4, 1, 5, 3, 2 ] ).perl;
Add Comment
Please, Sign In to add comment