Skip to content

Instantly share code, notes, and snippets.

@pmurias
Created October 14, 2018 17:56
Show Gist options
  • Save pmurias/b66f6f0eb46f296b8a285c45a152f18c to your computer and use it in GitHub Desktop.
Save pmurias/b66f6f0eb46f296b8a285c45a152f18c to your computer and use it in GitHub Desktop.
Quicksort
# 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