Created
October 14, 2018 17:56
-
-
Save pmurias/b66f6f0eb46f296b8a285c45a152f18c to your computer and use it in GitHub Desktop.
Quicksort
This file contains hidden or 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
# Empty list sorts to the empty list | |
multi quicksort([]) { () } | |
# Otherwise, extract first item as pivot... | |
multi quicksort([$pivot, *@rest]) { | |
# Partition. | |
my $before := @rest.grep(* before $pivot); | |
my $after := @rest.grep(* !before $pivot); | |
# Sort the partitions. | |
flat quicksort($before), $pivot, quicksort($after) | |
} | |
say quicksort([4,5,7,9]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment