-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.pas
122 lines (112 loc) · 2.88 KB
/
sort.pas
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
# table_sort
a fusion sort algorithm & iterative to optimize memory // max is the table length // written in pascal
program testab2_0;
uses biblotab,crt;
const max=19;
var e,p,u,i,f,y,z,x,a:longint;
var T:Ttab; var V:array[1..max]of longint;
begin
clrscr;
alea1d(max,T);
for i:=1 to max do
begin
write(T[i],' | ');
end;
writeln;
a:=1;
while a<= (max div 2) do
begin
x:=1;
while x+2*a-1<=max do
begin
y:=x;
z:=x+a;
p:=x+2*a-1;
u:=x+a-1;
e:=x;
for f:=e to p do
begin
if (y<=u)and(z<=p) then
begin
if T[z]<T[y] then
begin
V[f]:=T[z];
z:=z+1;
end
else
begin
V[f]:=T[y];
y:=y+1;
end;
end
else
begin
if z>p then
begin
V[f]:=T[y];
y:=y+1;
end
else
begin
V[f]:=T[z];
z:=z+1;
end;
end;
end;
x:=x+2*a;
end;
for i:=1 to x-1 do
begin
T[i]:=V[i];
V[i]:=0;
end;
if max mod (2*a)<>0 then (* le tri final *)
begin
p:=max;
z:=x;
u:=x-1;
y:=x-2*a;
for f:=x-2*a to max do
begin
if (y<=u)and(z<=p) then
begin
if T[z]<T[y] then
begin
V[f]:=T[z];
z:=z+1;
end
else
begin
V[f]:=T[y];
y:=y+1;
end;
end
else
begin
if z>p then
begin
V[f]:=T[y];
y:=y+1;
end
else
begin
V[f]:=T[z];
z:=z+1;
end;
end;
end;
for i:=x-2*a to max do
begin
T[i]:=V[i];
V[i]:=0;
end;
end;
a:=a*2;
end;
writeln;
for i:=1 to max do
begin
write(T[i],' | ');
end;
readln;
end.