Skip to content

Instantly share code, notes, and snippets.

@Puriney
Forked from puriney2/MergeSort.PL
Created July 25, 2013 19:23
Show Gist options
  • Save Puriney/6082902 to your computer and use it in GitHub Desktop.
Save Puriney/6082902 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