I implemented quicksort
in OCaml. Here is the code:
let shuffle d =
let nd = List.map (fun c -> (Random.bits (), c)) d in
let sond = List.sort compare nd in
List.map snd sond;;
let partition = function
| [] -> ([], [], [])
| pivot::tl ->
let rec p (left, right) = function
| [] -> (left, right, [pivot])
| first::rest ->
let c = compare pivot first
in
if c > 0 then
p (first::left, right) rest
else
p (left, first::right) rest
in
p ([], []) tl;;
let quicksort l =
let sl = shuffle l
in
let rec qs = function
| [] -> []
| l ->
let (left, right, pivot) = partition l
in
(qs left) @ pivot @ (qs right)
in
qs sl;;
First, I think maybe there is a better way to implement partition. List.partition
came to my mind, but I just wanted to implement the key part by myself
Second, I use @
a lot in the sorting which is inefficient
, right?
any suggestions?
Edit
One more question to think about is whether 3-way quicksort
affects the implementation in OCaml?