Skip to content

Instantly share code, notes, and snippets.

@Ajwah
Created May 14, 2015 12:31
Show Gist options
  • Save Ajwah/d834a1d33fd6cb8f5d0b to your computer and use it in GitHub Desktop.
Save Ajwah/d834a1d33fd6cb8f5d0b to your computer and use it in GitHub Desktop.
efficient quicksort implementation
(*Source: http://www.webber-labs.com/mpl/source%20code/Chapter%20Twelve/quicksort.sml.txt *)
fun quicksort nil = nil
| quicksort (pivot :: rest) =
let
fun split(nil) = (nil,nil)
| split(x :: xs) =
let
val (below, above) = split(xs)
in
if x < pivot then (x :: below, above)
else (below, x :: above)
end;
val (below, above) = split(rest)
in
quicksort below @ [pivot] @ quicksort above
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment