-
-
Save Puriney/6082902 to your computer and use it in GitHub Desktop.
PERL: Merge Sort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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