Commit 1f71a51d authored by Taddeus Kroes's avatar Taddeus Kroes

funclang series4: Improved quickSort.

parent cf3cb8a8
open List
(* Sort a list l using the quicksort algorithm *) (* Sort a list l using the quicksort algorithm *)
let rec quickSort (l:int list) = let rec quickSort (l:int list) =
match l with match l with
| ([] | [_]) -> l | ([] | [_]) -> l
| pivot::rest -> | pivot::rest ->
let rec assign low high l = let cmp x = x < pivot in
let low, high = partition cmp rest in
quickSort low @ pivot :: quickSort high
(*let rec assign low high l =
match l with match l with
(* Assigning elements is done, sort the lower and higher sublists (* Assigning elements is done, sort the lower and higher sublists
* individually and concatenate them with the pivot *) * individually and concatenate them with the pivot *)
...@@ -13,11 +18,9 @@ let rec quickSort (l:int list) = ...@@ -13,11 +18,9 @@ let rec quickSort (l:int list) =
| h::t -> if h <= pivot then assign (h::low) high t | h::t -> if h <= pivot then assign (h::low) high t
else assign low (h::high) t else assign low (h::high) t
in in
assign [] [] rest asign [] [] rest*)
;; ;;
open List
(* Sort a list l using the merge sort algorithm *) (* Sort a list l using the merge sort algorithm *)
let rec mergeSort (l:int list) = let rec mergeSort (l:int list) =
match l with match l with
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment