-
Notifications
You must be signed in to change notification settings - Fork 1
/
mergeSort.erl
61 lines (55 loc) · 1.78 KB
/
mergeSort.erl
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
-module(mergeSort).
-export([start/0]).
%% 归并排序
%% 将?RANGE个数的随机序列排序:小->大
%% 由分入合,最后得到顺序序列
-define(RANGE,100).
start() ->
List = genList(?RANGE,0,[]),
io:format("The list is:~n~w~n",[List]),
Result = mergeSort(List),
io:format("The result is:~n~w~n", [Result]).
mergeSort(List) ->
Length = length(List),
Results = start(trunc(Length/2),List),
Results.
start(_MRef,List) when length(List) == 1 ->
List;
start(MRef,List) ->
{LeftL,RightL} = lists:split(MRef,List),
LeftResult = start(trunc(length(LeftL)/2),LeftL),
RightResult = start(trunc(length(RightL)/2),RightL),
Sorted = mergeL(1,LeftResult,1,RightResult,[]),
%io:format("Sorted is:~p~n",[Sorted]),
Sorted.
%% 合并
mergeL(ARef,ListA,BRef,ListB,Results) when ARef > length(ListA),BRef > length(ListB) ->
%io:format("Results is:~p~n",[Results]),
Results;
mergeL(ARef,ListA,BRef,ListB,Results) when ARef > length(ListA),BRef =< length(ListB) ->
NewResults = Results ++ lists:nthtail(BRef-1,ListB),
%io:format("New results is:~p~n",[NewResults]),
NewResults;
mergeL(ARef,ListA,BRef,ListB,Results) when BRef > length(ListB),ARef =< length(ListA) ->
NewResults = Results ++ lists:nthtail(ARef-1,ListA),
%io:format("New results is:~p~n",[NewResults]),
NewResults;
mergeL(ARef,ListA,BRef,ListB,Results) ->
AV = lists:nth(ARef,ListA),
BV = lists:nth(BRef,ListB),
%io:format("AV:~p,BV:~p~n",[AV,BV]),
%timer:sleep(5000),
if AV > BV ->
mergeL(ARef,ListA,BRef+1,ListB,Results ++ [BV]);
true ->
mergeL(ARef+1,ListA,BRef,ListB,Results ++ [AV])
end.
%% 生成随机数列
genList(Limit,Num,List) ->
case Num of
Limit ->
List;
_ ->
NewList = List ++ [rand:uniform(Limit)],
genList(Limit, Num+1, NewList)
end.