-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHEAPSORT.PAS
200 lines (140 loc) · 3.46 KB
/
HEAPSORT.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
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
{ HEAPSORT.PAS
The heapsort algorithm, for use with the Archetype interpreter. It uses
an xarray as its heap.
}
unit heapsort;
interface
uses misc, xarray;
type
heaptype = xarray_type;
element = pointer;
var
H : heaptype;
{ Functions and Procedures }
function pop_heap(var e : element) : boolean;
procedure drop_on_heap(e : element);
procedure drop_str_on_heap(var s : string);
procedure reinit_heap;
implementation
const
CANT_PEEK = 'Internal error: cannot peek into heap';
CANT_POKE = 'Internal error: cannot poke into heap';
function lighter(var one, two : element): boolean;
begin
lighter := string_ptr(one)^ < string_ptr(two)^
end;
procedure heapup;
var
L, parent : integer;
Lp, parentp : element;
temp : element;
begin
L := H.size;
while L > 1 do begin
if (L mod 2) = 0 then
parent := L div 2
else
parent := (L - 1) div 2;
if not (access_xarray(H, L, Lp, PEEK_ACCESS) and
access_xarray(H, parent, parentp, PEEK_ACCESS)) then
writeln(CANT_PEEK);
if lighter(Lp, parentp) then begin
temp := parentp;
if not (access_xarray(H, parent, Lp, POKE_ACCESS) and
access_xarray(H, L, temp, POKE_ACCESS))
then
writeln(CANT_POKE);
L := parent
end
else
L := 0
end
end; { heapup }
procedure heapdown;
var
L, compare, lc, rc : integer;
Lp : element;
lcp, rcp : element;
comparep : element;
temp : element;
begin
L := 1;
while L < H.size do begin
lc := L * 2;
if lc > H.size then
L := lc
else begin
rc := lc + 1;
if not access_xarray(H, lc, lcp, PEEK_ACCESS) then
writeln(CANT_PEEK);
if (rc > H.size) then begin
compare := lc;
comparep := lcp
end
else begin
if not access_xarray(H, rc, rcp, PEEK_ACCESS) then
writeln(CANT_PEEK);
if lighter(lcp, rcp) then begin
compare := lc;
comparep := lcp
end
else begin
compare := rc;
comparep := rcp
end
end;
if not access_xarray(H, L, lp, PEEK_ACCESS) then
writeln(CANT_PEEK);
if lighter(comparep, Lp) then begin
temp := comparep;
if not (access_xarray(H, compare, Lp, POKE_ACCESS) and
access_xarray(H, L, temp, POKE_ACCESS))
then
writeln(CANT_POKE);
L := compare
end
else
L := H.size + 1
end
end
end; { heapdown }
function pop_heap(var e : element) : boolean;
var
temp : element;
begin
if H.size < 1 then
pop_heap := FALSE
else begin
if not (access_xarray(H, 1, e, PEEK_ACCESS) and
access_xarray(H, H.size, temp, PEEK_ACCESS) and
access_xarray(H, 1, temp, POKE_ACCESS))
then
writeln(CANT_PEEK);
shrink_xarray(H);
heapdown;
pop_heap := TRUE
end
end;
procedure drop_on_heap(e : element);
begin
append_to_xarray(H, e);
heapup
end;
procedure drop_str_on_heap(var s : string);
var
sp : string_ptr;
p : pointer;
begin
sp := NewDynStr(s);
p := pointer(sp);
drop_on_heap(p)
end;
procedure reinit_heap;
begin
dispose_xarray(H);
new_xarray(H)
end;
begin
new_xarray(H)
end.