-
Notifications
You must be signed in to change notification settings - Fork 0
/
smlexp_1.sml
36 lines (31 loc) · 865 Bytes
/
smlexp_1.sml
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
fun printInt (a:int) =
print(Int.toString(a)^" ");
fun getInt () =
Option.valOf (TextIO.scanStream (Int.scan StringCvt.DEC) TextIO.stdIn);
fun printIntTable ( [] ) = ()
| printIntTable ( x::xs ) =
let
val tmp = printInt(x)
in
printIntTable(xs)
end;
fun getIntTable ( 0 ) = []
| getIntTable ( N:int) = getInt()::getIntTable(N-1);
(*****Begin*****)
val n:int=getInt();
val list:(int)list=getIntTable(n);
fun mergesort [] = []
|mergesort [Li] = [Li]
|mergesort(Li:(int)list)=
let
val mid:int = length(Li) div 2
fun merge([],y)=y (*从小到大依次进行merge*)
|merge(x,[])=x
|merge(x::xs,y::ys)=
if x<y then x::merge(xs,y::ys)
else y::merge(x::xs,ys)
in merge(mergesort(List.take(Li,mid)),mergesort(List.drop(Li,mid)))
end;
val result:(int)list=mergesort(list);
printIntTable(result);
(*****End*****)