Created
June 29, 2017 08:19
-
-
Save tiensonqin/1114cf5df8f10f70b4b404ce1b001386 to your computer and use it in GitHub Desktop.
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
open Utils | |
let imperative_sort a = | |
let l = Array.length a in | |
for i = 0 to (l - 1) do | |
let min_idx = ref i in | |
for j = i + 1 to (l - 1) do | |
if (a.(j) < a.(!min_idx)) then min_idx := j | |
done; | |
if !min_idx != i then swap a i !min_idx | |
done | |
let rec find_min ?(acc = []) = function | |
| h1 :: h2 :: tl when h1 > h2 -> | |
find_min ~acc:(h1 :: acc) (h2 :: tl) | |
| h1 :: h2 :: tl -> | |
find_min ~acc:(h2 :: acc) (h1 :: tl) | |
| [x] -> (x, acc) | |
| [] -> raise Not_found | |
let rec persistent_sort ?(acc = []) = function | |
| [] -> List.rev acc | |
| s -> | |
let (min, rest) = find_min s in | |
persistent_sort ~acc:(min :: acc) rest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment