Skip to content

Instantly share code, notes, and snippets.

@puriney2
Created July 15, 2013 02:26
Show Gist options
  • Save puriney2/5997110 to your computer and use it in GitHub Desktop.
Save puriney2/5997110 to your computer and use it in GitHub Desktop.
PERL: Merge Sort
# http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#Perl
sub merge_sort {
my @x = @_;
return @x if @x < 2;
my $m = int @x / 2;
my @a = merge_sort(@x[0 .. $m - 1]);
my @b = merge_sort(@x[$m .. $#x]);
for (@x) {
$_ =
!@a ? shift @b :
!@b ? shift @a :
$a[0] <= $b[0] ? shift @a :
shift @b;
}
@x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment