forked from colinbenner/ocaml-llvm
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsorts.ml
4477 lines (4244 loc) · 122 KB
/
sorts.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* Test bench for sorting algorithms. *)
(*
ocamlopt -noassert sorts.ml -cclib -lunix
*)
open Printf;;
(*
Criteres:
0. overhead en pile: doit etre logn au maximum.
1. stable ou non.
2. overhead en espace.
3. vitesse.
*)
(************************************************************************)
(* auxiliary functions *)
let rec exp2 n = if n <= 0 then 1 else 2 * exp2 (n-1);;
let id x = x;;
let postl x y = Array.of_list y;;
let posta x y = x;;
let mkconst n = Array.make n 0;;
let chkconst _ n a = (a = mkconst n);;
let mksorted n =
let a = Array.make n 0 in
for i = 0 to n - 1 do
a.(i) <- i;
done;
a
;;
let chksorted _ n a = (a = mksorted n);;
let mkrev n =
let a = Array.make n 0 in
for i = 0 to n - 1 do
a.(i) <- n - 1 - i;
done;
a
;;
let chkrev _ n a = (a = mksorted n);;
let seed = ref 0;;
let random_reinit () = Random.init !seed;;
let random_get_state () =
let a = Array.make 55 0 in
for i = 0 to 54 do a.(i) <- Random.bits (); done;
Random.full_init a;
a
;;
let random_set_state a = Random.full_init a;;
let chkgen mke cmp rstate n a =
let marks = Array.make n (-1) in
let skipmarks l =
if marks.(l) = -1 then l else begin
let m = ref marks.(l) in
while marks.(!m) <> -1 do incr m; done;
marks.(l) <- !m;
!m
end
in
let linear e l =
let l = skipmarks l in
let rec loop l =
if cmp a.(l) e > 0 then raise Exit
else if e = a.(l) then marks.(l) <- l+1
else loop (l+1)
in loop l
in
let rec dicho e l r =
if l = r then linear e l
else begin
assert (l < r);
let m = (l + r) / 2 in
if cmp a.(m) e >= 0 then dicho e l m else dicho e (m + 1) r
end
in
try
for i = 0 to n-2 do if cmp a.(i) a.(i+1) > 0 then raise Exit; done;
random_set_state rstate;
for i = 0 to n-1 do dicho (mke i) 0 (Array.length a - 1); done;
true
with Exit | Invalid_argument _ -> false;
;;
let mkrand_dup n =
let a = Array.make n 0 in
for i = 0 to (n-1) do a.(i) <- Random.int n; done;
a
;;
let chkrand_dup rstate n a =
chkgen (fun i -> Random.int n) compare rstate n a
;;
let mkrand_nodup n =
let a = Array.make n 0 in
for i = 0 to (n-1) do a.(i) <- Random.bits (); done;
a
;;
let chkrand_nodup rstate n a =
chkgen (fun i -> Random.bits ()) compare rstate n a
;;
let mkfloats n =
let a = Array.make n 0.0 in
for i = 0 to (n-1) do a.(i) <- Random.float 1.0; done;
a
;;
let chkfloats rstate n a =
chkgen (fun i -> Random.float 1.0) compare rstate n a
;;
type record = {
s1 : string;
s2 : string;
i1 : int;
i2 : int;
};;
let rand_string () =
let len = Random.int 10 in
let s = String.create len in
for i = 0 to len-1 do
s.[i] <- Char.chr (Random.int 256);
done;
s
;;
let mkrec1 b i = {
s1 = rand_string ();
s2 = rand_string ();
i1 = Random.int b;
i2 = i;
};;
let mkrecs b n = Array.init n (mkrec1 b);;
let mkrec1_rev b i = {
s1 = rand_string ();
s2 = rand_string ();
i1 = - i;
i2 = i;
};;
let mkrecs_rev n = Array.init n (mkrec1_rev 0);;
let cmpstr r1 r2 =
let c1 = compare r1.s1 r2.s1 in
if c1 = 0 then compare r1.s2 r2.s2 else c1
;;
let lestr r1 r2 =
let c1 = compare r1.s1 r2.s1 in
if c1 = 0 then r1.s2 <= r2.s2 else (c1 < 0)
;;
let chkstr b rstate n a = chkgen (mkrec1 b) cmpstr rstate n a;;
let cmpint r1 r2 = compare r1.i1 r2.i1;;
let leint r1 r2 = r1.i1 <= r2.i1;;
let chkint b rstate n a = chkgen (mkrec1 b) cmpint rstate n a;;
let cmplex r1 r2 =
let c1 = compare r1.i1 r2.i1 in
if c1 = 0 then compare r1.i2 r2.i2 else c1
;;
let lelex r1 r2 =
let c1 = compare r1.i1 r2.i1 in
if c1 = 0 then r1.i2 <= r2.i2 else (c1 < 0)
;;
let chklex b rstate n a = chkgen (mkrec1 b) cmplex rstate n a;;
(************************************************************************)
let lens = [
0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 28;
100; 127; 128; 129; 191; 192; 193; 506;
1000; 1023; 1024; 1025; 1535; 1536; 1537; 2323;
4000; 4094; 4096; 4098; 5123;
];;
type ('a, 'b, 'c, 'd) aux = {
prepf : ('a -> 'a -> int) -> ('a -> 'a -> bool) -> 'b;
prepd : 'a array -> 'c;
postd : 'a array -> 'd -> 'a array;
};;
let ll = { prepf = (fun x y -> y); prepd = Array.to_list; postd = postl };;
let lc = { prepf = (fun x y -> x); prepd = Array.to_list; postd = postl };;
let al = { prepf = (fun x y -> y); prepd = id; postd = posta };;
let ac = { prepf = (fun x y -> x); prepd = id; postd = posta };;
type 'a outcome = Value of 'a | Exception of exn;;
let numfailed = ref 0;;
let test1 name f prepdata postdata cmp desc mk chk =
random_reinit ();
printf " %s with %s" name desc;
let i = ref 0 in
List.iter (fun n ->
if !i = 0 then printf "\n "; incr i; if !i > 11 then i := 0;
printf "%5d" n; flush stdout;
let rstate = random_get_state () in
let a = mk n in
let input = prepdata a in
let output = try Value (f cmp input) with e -> Exception e in
printf "."; flush stdout;
begin match output with
| Value v ->
if not (chk rstate n (postdata a v))
then (incr numfailed; printf "\n*** FAIL\n")
| Exception e ->
incr numfailed; printf "\n*** %s\n" (Printexc.to_string e)
end;
flush stdout;
) lens;
printf "\n";
;;
let test name stable f1 f2 aux1 aux2 =
printf "Testing %s...\n" name;
let t a b c d = test1 name f1 aux1.prepd aux1.postd a b c d in
let cmp = aux1.prepf compare (<=) in
t cmp "constant ints" mkconst chkconst;
t cmp "sorted ints" mksorted chksorted;
t cmp "reverse-sorted ints" mkrev chkrev;
t cmp "random ints (many dups)" mkrand_dup chkrand_dup;
t cmp "random ints (few dups)" mkrand_nodup chkrand_nodup;
(*
let t a b c d = test1 name f3 aux3.prepd aux3.postd a b c d in
t cmp "random floats" mkfloats chkfloats;
*)
let t a b c d = test1 name f2 aux2.prepd aux2.postd a b c d in
let cmp = aux2.prepf cmpstr lestr in
t cmp "records (str)" (mkrecs 1) (chkstr 1);
let cmp = aux2.prepf cmpint leint in
List.iter (fun m -> t cmp (sprintf "records (int[%d])" m) (mkrecs m)
(chkint m)
) [1; 10; 100; 1000];
if stable then
List.iter (fun m -> t cmp (sprintf "records (int[%d]) [stable]" m)
(mkrecs m) (chklex m)
) [1; 10; 100; 1000];
;;
(************************************************************************)
(* Warning: rpt_timer cannot be used for the array sorts because
the sorting functions have effects.
*)
let rpt_timer1 repeat f x =
Gc.compact ();
ignore (f x);
let st = Sys.time () in
for i = 1 to repeat do ignore (f x); done;
let en = Sys.time () in
en -. st
;;
let rpt_timer f x =
let repeat = ref 1 in
let t = ref (rpt_timer1 !repeat f x) in
while !t < 0.2 do
repeat := 10 * !repeat;
t := rpt_timer1 !repeat f x;
done;
if !t < 2.0 then begin
repeat := (int_of_float (10. *. (float !repeat) /. !t) + 1);
t := rpt_timer1 !repeat f x;
end;
!t /. (float !repeat)
;;
let timer f x =
let st = Sys.time () in
ignore (f x);
let en = Sys.time () in
(en -. st)
;;
let table1 limit f mkarg =
printf " %10s %9s %9s %9s %9s %9s\n" "n" "t1" "t2" "t3" "t4" "t5";
let sz = ref 49151 in
while !sz < int_of_float (2. ** float limit) do
begin try
printf " %10d " !sz; flush stdout;
for i = 0 to 4 do
let arg = mkarg !sz in
let t = timer f arg in
printf " %.2e " t; flush stdout;
done;
printf "\n";
with e -> printf "*** %s\n" (Printexc.to_string e);
end;
flush stdout;
sz := 2 * !sz + 1;
done;
;;
let table2 limit f mkarg =
printf " %10s %9s %9s %9s %9s %9s\n"
" n" "t" "t/n" "t/nlogn" "t/nlog^2n" "t/n^2";
let sz = ref 49151 in
while float !sz < 2. ** float limit do
begin try
printf " %10d " !sz; flush stdout;
Gc.compact ();
let arg = mkarg !sz in
let t = timer f arg in
let n = float !sz in
let logn = log (float !sz) /. log 2. in
printf "%.2e %.2e %.2e %.2e %.2e\n"
t (t/.n) (t/.n/.logn) (t/.n/.logn/.logn) (t/.n/.n);
with e -> printf "*** %s\n" (Printexc.to_string e);
end;
flush stdout;
sz := 2 * !sz + 1;
done;
;;
let table3 limit f mkarg =
printf " %10s %9s %9s %9s %9s %9s\n" "n" "t1" "t2" "t3" "t4" "t5";
let sz = ref 2 in
while float !sz < 2. ** float limit do
begin try
printf " %10d " !sz; flush stdout;
for i = 0 to 4 do
let arg = mkarg !sz in
let t = rpt_timer f arg in
printf " %.2e " t; flush stdout;
done;
printf "\n";
with e -> printf "*** %s\n" (Printexc.to_string e);
end;
flush stdout;
sz := 2 * !sz + 1;
done;
;;
(************************************************************************)
(* benchmarks:
1a. random records, sorted with two keys
1b. random integers
1c. random floats
2a. integers, constant
2b. integers, already sorted
2c. integers, reverse sorted
only for short lists:
3a. random records, sorted with two keys
3b. random integers
3c. random floats
*)
let bench1a limit name f aux =
(* Don't do benchmarks with assertions enabled. *)
assert (not true);
random_reinit ();
printf "\n%s with random records [10]:\n" name;
let cmp = aux.prepf cmplex lelex in
table1 limit (f cmp) (fun n -> aux.prepd (mkrecs 10 n));
;;
let bench1b limit name f aux =
(* Don't do benchmarks with assertions enabled. *)
assert (not true);
random_reinit ();
printf "\n%s with random integers:\n" name;
let cmp = aux.prepf (-) (<=) in
table1 limit (f cmp) (fun n -> aux.prepd (mkrand_nodup n));
;;
let bench1c limit name f aux =
(* Don't do benchmarks with assertions enabled. *)
assert (not true);
random_reinit ();
printf "\n%s with random floats:\n" name;
let cmp = aux.prepf compare (<=) in
table1 limit (f cmp) (fun n -> aux.prepd (mkfloats n));
;;
let bench2 limit name f aux =
(* Don't do benchmarks with assertions enabled. *)
assert (not true);
printf "\n%s with constant integers:\n" name;
let cmp = aux.prepf compare (<=) in
table2 limit (f cmp) (fun n -> aux.prepd (mkconst n));
printf "\n%s with sorted integers:\n" name;
let cmp = aux.prepf compare (<=) in
table2 limit (f cmp) (fun n -> aux.prepd (mksorted n));
printf "\n%s with reverse-sorted integers:\n" name;
let cmp = aux.prepf compare (<=) in
table2 limit (f cmp) (fun n -> aux.prepd (mkrev n));
;;
let bench3a limit name f aux =
(* Don't do benchmarks with assertions enabled. *)
assert (not true);
random_reinit ();
printf "\n%s with random records [10]:\n" name;
let cmp = aux.prepf cmplex lelex in
table3 limit (f cmp) (fun n -> aux.prepd (mkrecs 10 n));
;;
let bench3b limit name f aux =
(* Don't do benchmarks with assertions enabled. *)
assert (not true);
random_reinit ();
printf "\n%s with random integers:\n" name;
let cmp = aux.prepf (-) (<=) in
table3 limit (f cmp) (fun n -> aux.prepd (mkrand_nodup n));
;;
let bench3c limit name f aux =
(* Don't do benchmarks with assertions enabled. *)
assert (not true);
random_reinit ();
printf "\n%s with random floats:\n" name;
let cmp = aux.prepf compare (<=) in
table3 limit (f cmp) (fun n -> aux.prepd (mkfloats n));
;;
(************************************************************************)
(* merge sort on lists *)
(* FIXME to do: cutoff
to do: cascader les pattern-matchings (enlever les paires)
to do: fermeture intermediaire pour merge
*)
let (@@) = List.rev_append;;
let lmerge_1a cmp l =
let rec init accu = function
| [] -> accu
| e::rest -> init ([e] :: accu) rest
in
let rec merge rest accu2 accu l1 l2 = (* l1,l2,rest are forward;
accu,accu2 are rev *)
match l1, l2 with
| [] , _ -> mergepairs ((l2 @@ accu)::accu2) rest
| _ , [] -> mergepairs ((l1 @@ accu)::accu2) rest
| h1::t1, h2::t2 -> if cmp h1 h2 <= 0
then merge rest accu2 (h1::accu) t1 l2
else merge rest accu2 (h2::accu) l1 t2
and merge_rev rest accu2 accu l1 l2 = (* accu, accu2 are forward;
l1,l2,rest are rev *)
match l1, l2 with
| [] , _ -> mergepairs_rev ((l2 @@ accu)::accu2) rest
| _ , [] -> mergepairs_rev ((l1 @@ accu)::accu2) rest
| h1::t1, h2::t2 -> if cmp h2 h1 <= 0
then merge_rev rest accu2 (h1::accu) t1 l2
else merge_rev rest accu2 (h2::accu) l1 t2
and mergepairs accu = function (* accu is rev, arg is forward *)
| [] -> mergeall_rev accu
| [l] -> mergeall_rev ((List.rev l)::accu)
| l1::l2::rest -> merge rest accu [] l1 l2
and mergepairs_rev accu = function (* accu is forward, arg is rev *)
| [] -> mergeall accu
| [l] -> mergeall ((List.rev l)::accu)
| l1::l2::rest -> merge_rev rest accu [] l1 l2
and mergeall = function (* arg is forward *)
| [] -> []
| [l] -> l
| llist -> mergepairs [] llist
and mergeall_rev = function (* arg is rev *)
| [] -> []
| [l] -> List.rev l
| llist -> mergepairs_rev [] llist
in
mergeall_rev (init [] l)
;;
let lmerge_1b cmp l =
let rec init accu = function
| [] -> accu
| [e] -> [e] :: accu
| e1::e2::rest ->
init ((if cmp e1 e2 <= 0 then [e2;e1] else [e1;e2])::accu) rest
in
let rec merge rest accu2 accu l1 l2 = (* l1,l2,rest are forward;
accu,accu2 are rev *)
match l1, l2 with
| [] , _ -> mergepairs ((l2 @@ accu)::accu2) rest
| _ , [] -> mergepairs ((l1 @@ accu)::accu2) rest
| h1::t1, h2::t2 -> if cmp h1 h2 <= 0
then merge rest accu2 (h1::accu) t1 l2
else merge rest accu2 (h2::accu) l1 t2
and merge_rev rest accu2 accu l1 l2 = (* accu, accu2 are forward;
l1,l2,rest are rev *)
match l1, l2 with
| [] , _ -> mergepairs_rev ((l2 @@ accu)::accu2) rest
| _ , [] -> mergepairs_rev ((l1 @@ accu)::accu2) rest
| h1::t1, h2::t2 -> if cmp h2 h1 <= 0
then merge_rev rest accu2 (h1::accu) t1 l2
else merge_rev rest accu2 (h2::accu) l1 t2
and mergepairs accu = function (* accu is rev, arg is forward *)
| [] -> mergeall_rev accu
| [l] -> mergeall_rev ((List.rev l)::accu)
| l1::l2::rest -> merge rest accu [] l1 l2
and mergepairs_rev accu = function (* accu is forward, arg is rev *)
| [] -> mergeall accu
| [l] -> mergeall ((List.rev l)::accu)
| l1::l2::rest -> merge_rev rest accu [] l1 l2
and mergeall = function (* arg is forward *)
| [] -> []
| [l] -> l
| llist -> mergepairs [] llist
and mergeall_rev = function (* arg is rev *)
| [] -> []
| [l] -> List.rev l
| llist -> mergepairs_rev [] llist
in
mergeall_rev (init [] l)
;;
let lmerge_1c cmp l =
let rec init accu = function
| [] -> accu
| [e] -> [e] :: accu
| e1::e2::rest ->
init ((if cmp e1 e2 <= 0 then [e2;e1] else [e1;e2])::accu) rest
in
let rec merge rest accu2 accu l1 l2 = (* l1,l2,rest are forward;
accu,accu2 are rev *)
match l1 with
| [] -> mergepairs ((l2 @@ accu)::accu2) rest
| h1::t1 ->
match l2 with
| [] -> mergepairs ((l1 @@ accu)::accu2) rest
| h2::t2 -> if cmp h1 h2 <= 0
then merge rest accu2 (h1::accu) t1 l2
else merge rest accu2 (h2::accu) l1 t2
and merge_rev rest accu2 accu l1 l2 = (* accu, accu2 are forward;
l1,l2,rest are rev *)
match l1 with
| [] -> mergepairs_rev ((l2 @@ accu)::accu2) rest
| h1::t1 ->
match l2 with
| [] -> mergepairs_rev ((l1 @@ accu)::accu2) rest
| h2::t2 -> if cmp h2 h1 <= 0
then merge_rev rest accu2 (h1::accu) t1 l2
else merge_rev rest accu2 (h2::accu) l1 t2
and mergepairs accu = function (* accu is rev, arg is forward *)
| [] -> mergeall_rev accu
| [l] -> mergeall_rev ((List.rev l)::accu)
| l1::l2::rest -> merge rest accu [] l1 l2
and mergepairs_rev accu = function (* accu is forward, arg is rev *)
| [] -> mergeall accu
| [l] -> mergeall ((List.rev l)::accu)
| l1::l2::rest -> merge_rev rest accu [] l1 l2
and mergeall = function (* arg is forward *)
| [] -> []
| [l] -> l
| llist -> mergepairs [] llist
and mergeall_rev = function (* arg is rev *)
| [] -> []
| [l] -> List.rev l
| llist -> mergepairs_rev [] llist
in
mergeall_rev (init [] l)
;;
let lmerge_1d cmp l =
let rec init accu = function
| [] -> accu
| [e] -> [e] :: accu
| e1::e2::rest ->
init ((if cmp e1 e2 <= 0 then [e2;e1] else [e1;e2])::accu) rest
in
let rec merge rest accu2 accu l1 l2 = (* l1,l2,rest are forward;
accu,accu2 are rev *)
let merge_rest_accu2 accu l1 l2 =
match l1 with
| [] -> mergepairs ((l2 @@ accu)::accu2) rest
| h1::t1 ->
match l2 with
| [] -> mergepairs ((l1 @@ accu)::accu2) rest
| h2::t2 -> if cmp h1 h2 <= 0
then merge rest accu2 (h1::accu) t1 l2
else merge rest accu2 (h2::accu) l1 t2
in merge_rest_accu2 accu l1 l2
and merge_rev rest accu2 accu l1 l2 = (* accu, accu2 are forward;
l1,l2,rest are rev *)
let merge_rev_rest_accu2 accu l1 l2 =
match l1 with
| [] -> mergepairs_rev ((l2 @@ accu)::accu2) rest
| h1::t1 ->
match l2 with
| [] -> mergepairs_rev ((l1 @@ accu)::accu2) rest
| h2::t2 -> if cmp h2 h1 <= 0
then merge_rev rest accu2 (h1::accu) t1 l2
else merge_rev rest accu2 (h2::accu) l1 t2
in merge_rev_rest_accu2 accu l1 l2
and mergepairs accu = function (* accu is rev, arg is forward *)
| [] -> mergeall_rev accu
| [l] -> mergeall_rev ((List.rev l)::accu)
| l1::l2::rest -> merge rest accu [] l1 l2
and mergepairs_rev accu = function (* accu is forward, arg is rev *)
| [] -> mergeall accu
| [l] -> mergeall ((List.rev l)::accu)
| l1::l2::rest -> merge_rev rest accu [] l1 l2
and mergeall = function (* arg is forward *)
| [] -> []
| [l] -> l
| llist -> mergepairs [] llist
and mergeall_rev = function (* arg is rev *)
| [] -> []
| [l] -> List.rev l
| llist -> mergepairs_rev [] llist
in
mergeall_rev (init [] l)
;;
(************************************************************************)
(* merge sort on lists, user-contributed (NOT STABLE) *)
(* BEGIN code contributed by Yann Coscoy *)
let rec rev_merge_append order l1 l2 acc =
match l1 with
[] -> List.rev_append l2 acc
| h1 :: t1 ->
match l2 with
[] -> List.rev_append l1 acc
| h2 :: t2 ->
if order h1 h2
then rev_merge_append order t1 l2 (h1::acc)
else rev_merge_append order l1 t2 (h2::acc)
let rev_merge order l1 l2 = rev_merge_append order l1 l2 []
let rec rev_merge_append' order l1 l2 acc =
match l1 with
| [] -> List.rev_append l2 acc
| h1 :: t1 ->
match l2 with
| [] -> List.rev_append l1 acc
| h2 :: t2 ->
if order h2 h1
then rev_merge_append' order t1 l2 (h1::acc)
else rev_merge_append' order l1 t2 (h2::acc)
let rev_merge' order l1 l2 = rev_merge_append' order l1 l2 []
let lmerge_3 order l =
let rec initlist l acc = match l with
| e1::e2::rest ->
initlist rest
((if order e1 e2 then [e1;e2] else [e2;e1])::acc)
| [e] -> [e]::acc
| [] -> acc
in
let rec merge2 ll acc = match ll with
| [] -> acc
| [l] -> [List.rev l]@acc
| l1::l2::rest ->
merge2 rest (rev_merge order l1 l2::acc)
in
let rec merge2' ll acc = match ll with
| [] -> acc
| [l] -> [List.rev l]@acc
| l1::l2::rest ->
merge2' rest (rev_merge' order l1 l2::acc)
in
let rec mergeall rev = function
| [] -> []
| [l] -> if rev then List.rev l else l
| llist ->
mergeall
(not rev) ((if rev then merge2' else merge2) llist [])
in
mergeall false (initlist l [])
(* END code contributed by Yann Coscoy *)
(************************************************************************)
(* merge sort on short lists, Francois Pottier *)
(* BEGIN code contributed by Francois Pottier *)
(* [chop k l] returns the list [l] deprived of its [k] first
elements. The length of the list [l] must be [k] at least. *)
let rec chop k l =
match k, l with
| 0, _ -> l
| _, x :: l -> chop (k-1) l
| _, _ -> assert false
;;
let rec merge order l1 l2 =
match l1 with
[] -> l2
| h1 :: t1 ->
match l2 with
[] -> l1
| h2 :: t2 ->
if order h1 h2
then h1 :: merge order t1 l2
else h2 :: merge order l1 t2
;;
let rec lmerge_4a order l =
match l with
| []
| [ _ ] -> l
| _ ->
let rec sort k l = (* k > 1 *)
match k, l with
| 2, x1 :: x2 :: _ ->
if order x1 x2 then [ x1; x2 ] else [ x2; x1 ]
| 3, x1 :: x2 :: x3 :: _ ->
if order x1 x2 then
if order x2 x3 then
[ x1 ; x2 ; x3 ]
else
if order x1 x3 then [ x1 ; x3 ; x2 ] else [ x3; x1; x2 ]
else
if order x1 x3 then
[ x2; x1; x3 ]
else
if order x2 x3 then [ x2; x3; x1 ] else [ x3; x2; x1 ]
| _, _ ->
let k1 = k / 2 in
let k2 = k - k1 in
merge order (sort k1 l) (sort k2 (chop k1 l))
in
sort (List.length l) l
;;
(* END code contributed by Francois Pottier *)
(************************************************************************)
(* merge sort on short lists, Francois Pottier,
adapted to new-style interface *)
(* BEGIN code contributed by Francois Pottier *)
(* [chop k l] returns the list [l] deprived of its [k] first
elements. The length of the list [l] must be [k] at least. *)
let rec chop k l =
match k, l with
| 0, _ -> l
| _, x :: l -> chop (k-1) l
| _, _ -> assert false
;;
let rec merge order l1 l2 =
match l1 with
[] -> l2
| h1 :: t1 ->
match l2 with
[] -> l1
| h2 :: t2 ->
if order h1 h2 <= 0
then h1 :: merge order t1 l2
else h2 :: merge order l1 t2
;;
let rec lmerge_4b order l =
match l with
| []
| [ _ ] -> l
| _ ->
let rec sort k l = (* k > 1 *)
match k, l with
| 2, x1 :: x2 :: _ ->
if order x1 x2 <= 0 then [ x1; x2 ] else [ x2; x1 ]
| 3, x1 :: x2 :: x3 :: _ ->
if order x1 x2 <= 0 then
if order x2 x3 <= 0 then
[ x1 ; x2 ; x3 ]
else
if order x1 x3 <= 0 then [ x1 ; x3 ; x2 ] else [ x3; x1; x2 ]
else
if order x1 x3 <= 0 then
[ x2; x1; x3 ]
else
if order x2 x3 <= 0 then [ x2; x3; x1 ] else [ x3; x2; x1 ]
| _, _ ->
let k1 = k / 2 in
let k2 = k - k1 in
merge order (sort k1 l) (sort k2 (chop k1 l))
in
sort (List.length l) l
;;
(* END code contributed by Francois Pottier *)
(************************************************************************)
(* merge sort on short lists a la Pottier, modified merge *)
let rec chop k l =
if k = 0 then l else begin
match l with
| x::t -> chop (k-1) t
| _ -> assert false
end
;;
let lmerge_4c cmp l =
let rec merge1 h1 t1 l2 =
match l2 with
| [] -> h1 :: t1
| h2 :: t2 ->
if cmp h1 h2 <= 0
then h1 :: (merge2 t1 h2 t2)
else h2 :: (merge1 h1 t1 t2)
and merge2 l1 h2 t2 =
match l1 with
| [] -> h2 :: t2
| h1 :: t1 ->
if cmp h1 h2 <= 0
then h1 :: (merge2 t1 h2 t2)
else h2 :: (merge1 h1 t1 t2)
in
let merge l1 = function
| [] -> l1
| h2 :: t2 -> merge2 l1 h2 t2
in
let rec sort n l =
match n, l with
| 2, x1 :: x2 :: _ ->
if cmp x1 x2 <= 0 then [x1; x2] else [x2; x1]
| 3, x1 :: x2 :: x3 :: _ ->
if cmp x1 x2 <= 0 then begin
if cmp x2 x3 <= 0 then [x1; x2; x3]
else if cmp x1 x3 <= 0 then [x1; x3; x2]
else [x3; x1; x2]
end else begin
if cmp x1 x3 <= 0 then [x2; x1; x3]
else if cmp x2 x3 <= 0 then [x2; x3; x1]
else [x3; x2; x1]
end
| n, l ->
let n1 = n asr 1 in
let n2 = n - n1 in
merge (sort n1 l) (sort n2 (chop n1 l))
in
let len = List.length l in
if len < 2 then l else sort len l
;;
(************************************************************************)
(* merge sort on short lists a la Pottier, logarithmic stack space *)
let rec chop k l =
if k = 0 then l else begin
match l with
| x::t -> chop (k-1) t
| _ -> assert false
end
;;
let lmerge_4d cmp l =
let rec rev_merge l1 l2 accu =
match l1, l2 with
| [], l2 -> l2 @@ accu
| l1, [] -> l1 @@ accu
| h1::t1, h2::t2 ->
if cmp h1 h2 <= 0
then rev_merge t1 l2 (h1::accu)
else rev_merge l1 t2 (h2::accu)
in
let rec rev_merge_rev l1 l2 accu =
match l1, l2 with
| [], l2 -> l2 @@ accu
| l1, [] -> l1 @@ accu
| h1::t1, h2::t2 ->
if cmp h1 h2 > 0
then rev_merge_rev t1 l2 (h1::accu)
else rev_merge_rev l1 t2 (h2::accu)
in
let rec sort n l =
match n, l with
| 2, x1 :: x2 :: _ ->
if cmp x1 x2 <= 0 then [x1; x2] else [x2; x1]
| 3, x1 :: x2 :: x3 :: _ ->
if cmp x1 x2 <= 0 then begin
if cmp x2 x3 <= 0 then [x1; x2; x3]
else if cmp x1 x3 <= 0 then [x1; x3; x2]
else [x3; x1; x2]
end else begin
if cmp x1 x3 <= 0 then [x2; x1; x3]
else if cmp x2 x3 <= 0 then [x2; x3; x1]
else [x3; x2; x1]
end
| n, l ->
let n1 = n asr 1 in
let n2 = n - n1 in
rev_merge_rev (rev_sort n1 l) (rev_sort n2 (chop n1 l)) []
and rev_sort n l =
match n, l with
| 2, x1 :: x2 :: _ ->
if cmp x1 x2 > 0 then [x1; x2] else [x2; x1]
| 3, x1 :: x2 :: x3 :: _ ->
if cmp x1 x2 > 0 then begin
if cmp x2 x3 > 0 then [x1; x2; x3]
else if cmp x1 x3 > 0 then [x1; x3; x2]
else [x3; x1; x2]
end else begin
if cmp x1 x3 > 0 then [x2; x1; x3]
else if cmp x2 x3 > 0 then [x2; x3; x1]
else [x3; x2; x1]
end
| n, l ->
let n1 = n asr 1 in
let n2 = n - n1 in
rev_merge (sort n1 l) (sort n2 (chop n1 l)) []
in
let len = List.length l in
if len < 2 then l else sort len l
;;
(************************************************************************)
(* merge sort on short lists a la Pottier, logarithmic stack space,
in place: input list is freed as the output is being computed. *)
let rec chop k l =
if k = 0 then l else begin
match l with
| x::t -> chop (k-1) t
| _ -> assert false
end
;;
let lmerge_4e cmp l =
let rec rev_merge l1 l2 accu =
match l1, l2 with
| [], l2 -> l2 @@ accu
| l1, [] -> l1 @@ accu
| h1::t1, h2::t2 ->
if cmp h1 h2 <= 0
then rev_merge t1 l2 (h1::accu)
else rev_merge l1 t2 (h2::accu)
in
let rec rev_merge_rev l1 l2 accu =
match l1, l2 with
| [], l2 -> l2 @@ accu
| l1, [] -> l1 @@ accu
| h1::t1, h2::t2 ->
if cmp h1 h2 > 0
then rev_merge_rev t1 l2 (h1::accu)
else rev_merge_rev l1 t2 (h2::accu)
in
let rec sort n l =
match n, l with
| 2, x1 :: x2 :: _ ->
if cmp x1 x2 <= 0 then [x1; x2] else [x2; x1]
| 3, x1 :: x2 :: x3 :: _ ->
if cmp x1 x2 <= 0 then begin
if cmp x2 x3 <= 0 then [x1; x2; x3]
else if cmp x1 x3 <= 0 then [x1; x3; x2]
else [x3; x1; x2]
end else begin
if cmp x1 x3 <= 0 then [x2; x1; x3]
else if cmp x2 x3 <= 0 then [x2; x3; x1]
else [x3; x2; x1]
end
| n, l ->
let n1 = n asr 1 in
let n2 = n - n1 in
let l2 = chop n1 l in
let s1 = rev_sort n1 l in
let s2 = rev_sort n2 l2 in
rev_merge_rev s1 s2 []
and rev_sort n l =
match n, l with