Skip to content

Instantly share code, notes, and snippets.

@fetburner
Created July 28, 2018 11:25
Show Gist options
  • Select an option

  • Save fetburner/b163400e97049ee7e1937d7bd97e123b to your computer and use it in GitHub Desktop.

Select an option

Save fetburner/b163400e97049ee7e1937d7bd97e123b to your computer and use it in GitHub Desktop.
基数ソート
let radix_sort : int list -> int list = fun ns ->
let ns =
Array.fold_left (fun ns i ->
let l, r = List.partition (fun n -> n land (1 lsl i) = 0) ns in
l @ r) ns @@ Array.init 62 @@ fun i -> i in
let l, r = List.partition (fun n -> n land (1 lsl 62) = 0) ns in
r @ l;;
Random.self_init ();;
let true =
List.for_all (fun ns -> radix_sort ns = List.sort compare ns) @@
Array.to_list @@
Array.init 100 @@ fun _ ->
Array.to_list @@ Array.init 1000 @@ fun _ ->
Random.bits () * if Random.bool () then -1 else 1;;
let measure f =
let start = Sys.time () in
f ();
Sys.time () -. start;;
let l =
Array.to_list @@
Array.init 100000 @@ fun _ ->
Random.bits () * if Random.bool () then -1 else 1;;
measure @@ fun () -> ignore @@ radix_sort l;;
measure @@ fun () -> ignore @@ List.sort compare l;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment