-
Notifications
You must be signed in to change notification settings - Fork 1
/
orderStatistics.ml
86 lines (77 loc) · 2.34 KB
/
orderStatistics.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(*
**
** ocaml module LinearSort
**
** Description: Three linear time sorting algorithms!
**
** Author: Eray Ozkural (exa) <[email protected]>, (C) 2003
**
** Copyright: See COPYING file that comes with this distribution
**
*)
open Printf
let exchange a i j = let t = a.(i) in a.(i) <- a.(j); a.(j) <- t
let print_intarray a = Array.iter (fun x->printf "%d " x) a
let minimum a =
let min = ref a.(0) in
for i = 1 to Array.length a - 1 do
if !min > a.(i) then min := a.(i) else ()
done;
!min
exception Done
let partition a p r =
let x = a.(p) (* select pivot *)
and i = ref p
and j = ref (r: int) in
try
while true do
(* downward scan *)
while a.(!j) > x do j := !j - 1; done;
(* upward scan *)
while a.(!i) < x do i := !i + 1; done;
if !i < !j then
exchange a !i !j
else
raise Done
done; !j (* never reached! *)
with Done -> !j
(* make sure you initialize your random number generator prior to call! *)
let randomized_partition a p r =
let i = p + (Random.int (r-p)) in
(*printf "p=%d r=%d i=%d a[i]=%d \n" p r i a.(i);*)
exchange a p i;
partition a p r
let rec randomized_select (a: int array) p r i =
if p = r then a.(p)
else
begin
Random.self_init ();
let q = randomized_partition a p r in
let k = q - p + 1 in
(*printf "partitioned array is = "; print_intarray a; printf "\n";*)
if i <= k then
randomized_select a p q i
else
randomized_select a (q+1) r (i-k)
end
let median select a =
select a 0 (Array.length a - 1) (Array.length a / 2)
let test () =
printf "order statistics test \n";
begin
let a = [| 5; 13; 2; 25; 7; 16; 20; 8; 4 |] in
printf "a="; print_intarray a; printf "\n";
printf "minimum is %d \n" (minimum a);
printf "using randomized select \n";
printf "median is %d \n" (median randomized_select a);
printf "3rd smallest elt is %d \n" (randomized_select a 0 8 3);
printf "6th smallest elt is %d \n" (randomized_select a 0 8 6)
end;
begin
let a = [| 5; 13; 2; 25; 7; 16; 20; 8; 4 |] in
printf "a="; print_intarray a; printf "\n";
printf "using median-of-medians select \n";
printf "median is %d \n" (median randomized_select a);
printf "3rd smallest elt is %d \n" (randomized_select a 0 8 3);
printf "6th smallest elt is %d \n" (randomized_select a 0 8 6)
end