procedure MergeSort var Queue var QQ array boolean of Queue Q1 Q2 Queu

 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
procedure MergeSort(var Q: Queue);
var QQ: array[boolean] of Queue;
Q1, Q2 : Queue;
V : TValue;
i , all : boolean;
begin
i := false ;
QQ[i] := Q; { QQ[false] := Входная очередь }
Init (QQ[not i]); { QQ[true] := пусто }
repeat
if Empty(QQ[i]) then begin
i:=noti;
all := true
378end
else
all := false ;
V := Top(QQ[i]);
Pop(QQ[i]);
Init (Q1);
Init (Q2);
while not Empty(QQ[i]) do begin
if V > Top(QQ[i]) then
break;
Push(Q1, V);
V := Top(QQ[i]) ;
Pop(QQ[i])
end;
Push(Q1, V);
if not Empty(QQ[i]) then begin
all := false ;
V := Top(QQ[i]);
Pop(QQ[i]);
while not Empty(QQ[i]) do begin
if V > Top(QQ[i]) then
break;
Push(Q2, V);
V := Top(QQ[i]);
Pop(QQ[i])
end;
Push(Q2,V);
Merge(Q1, Q2, QQ[not i])
end
else begin
while not Empty(Q1) do begin
Push(QQ[not i], Top(Q1));
Pop(Q1)
end
end
until all ;
379Q := QQ[not i]
end;