1 |
281 |
jeremybenn |
------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- GNAT RUN-TIME COMPONENTS --
|
4 |
|
|
-- --
|
5 |
|
|
-- G N A T . H E A P _ S O R T _ A --
|
6 |
|
|
-- --
|
7 |
|
|
-- B o d y --
|
8 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 1995-2008, AdaCore --
|
10 |
|
|
-- --
|
11 |
|
|
-- GNAT is free software; you can redistribute it and/or modify it under --
|
12 |
|
|
-- terms of the GNU General Public License as published by the Free Soft- --
|
13 |
|
|
-- ware Foundation; either version 2, or (at your option) any later ver- --
|
14 |
|
|
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
|
15 |
|
|
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
|
16 |
|
|
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
|
17 |
|
|
-- for more details. You should have received a copy of the GNU General --
|
18 |
|
|
-- Public License distributed with GNAT; see file COPYING. If not, write --
|
19 |
|
|
-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
|
20 |
|
|
-- Boston, MA 02110-1301, USA. --
|
21 |
|
|
-- --
|
22 |
|
|
-- As a special exception, if other files instantiate generics from this --
|
23 |
|
|
-- unit, or you link this unit with other files to produce an executable, --
|
24 |
|
|
-- this unit does not by itself cause the resulting executable to be --
|
25 |
|
|
-- covered by the GNU General Public License. This exception does not --
|
26 |
|
|
-- however invalidate any other reasons why the executable file might be --
|
27 |
|
|
-- covered by the GNU Public License. --
|
28 |
|
|
-- --
|
29 |
|
|
-- GNAT was originally developed by the GNAT team at New York University. --
|
30 |
|
|
-- Extensive contributions were provided by Ada Core Technologies Inc. --
|
31 |
|
|
-- --
|
32 |
|
|
------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
pragma Compiler_Unit;
|
35 |
|
|
|
36 |
|
|
package body GNAT.Heap_Sort_A is
|
37 |
|
|
|
38 |
|
|
----------
|
39 |
|
|
-- Sort --
|
40 |
|
|
----------
|
41 |
|
|
|
42 |
|
|
-- We are using the classical heapsort algorithm (i.e. Floyd's Treesort3)
|
43 |
|
|
-- as described by Knuth ("The Art of Programming", Volume III, first
|
44 |
|
|
-- edition, section 5.2.3, p. 145-147) with the modification that is
|
45 |
|
|
-- mentioned in exercise 18. For more details on this algorithm, see
|
46 |
|
|
-- Robert B. K. Dewar PhD thesis "The use of Computers in the X-ray
|
47 |
|
|
-- Phase Problem". University of Chicago, 1968, which was the first
|
48 |
|
|
-- publication of the modification, which reduces the number of compares
|
49 |
|
|
-- from 2NlogN to NlogN.
|
50 |
|
|
|
51 |
|
|
procedure Sort (N : Natural; Move : Move_Procedure; Lt : Lt_Function) is
|
52 |
|
|
|
53 |
|
|
Max : Natural := N;
|
54 |
|
|
-- Current Max index in tree being sifted
|
55 |
|
|
|
56 |
|
|
procedure Sift (S : Positive);
|
57 |
|
|
-- This procedure sifts up node S, i.e. converts the subtree rooted
|
58 |
|
|
-- at node S into a heap, given the precondition that any sons of
|
59 |
|
|
-- S are already heaps. On entry, the contents of node S is found
|
60 |
|
|
-- in the temporary (index 0), the actual contents of node S on
|
61 |
|
|
-- entry are irrelevant. This is just a minor optimization to avoid
|
62 |
|
|
-- what would otherwise be two junk moves in phase two of the sort.
|
63 |
|
|
|
64 |
|
|
procedure Sift (S : Positive) is
|
65 |
|
|
C : Positive := S;
|
66 |
|
|
Son : Positive;
|
67 |
|
|
Father : Positive;
|
68 |
|
|
|
69 |
|
|
begin
|
70 |
|
|
-- This is where the optimization is done, normally we would do a
|
71 |
|
|
-- comparison at each stage between the current node and the larger
|
72 |
|
|
-- of the two sons, and continue the sift only if the current node
|
73 |
|
|
-- was less than this maximum. In this modified optimized version,
|
74 |
|
|
-- we assume that the current node will be less than the larger
|
75 |
|
|
-- son, and unconditionally sift up. Then when we get to the bottom
|
76 |
|
|
-- of the tree, we check parents to make sure that we did not make
|
77 |
|
|
-- a mistake. This roughly cuts the number of comparisons in half,
|
78 |
|
|
-- since it is almost always the case that our assumption is correct.
|
79 |
|
|
|
80 |
|
|
-- Loop to pull up larger sons
|
81 |
|
|
|
82 |
|
|
loop
|
83 |
|
|
Son := 2 * C;
|
84 |
|
|
exit when Son > Max;
|
85 |
|
|
|
86 |
|
|
if Son < Max and then Lt (Son, Son + 1) then
|
87 |
|
|
Son := Son + 1;
|
88 |
|
|
end if;
|
89 |
|
|
|
90 |
|
|
Move (Son, C);
|
91 |
|
|
C := Son;
|
92 |
|
|
end loop;
|
93 |
|
|
|
94 |
|
|
-- Loop to check fathers
|
95 |
|
|
|
96 |
|
|
while C /= S loop
|
97 |
|
|
Father := C / 2;
|
98 |
|
|
|
99 |
|
|
if Lt (Father, 0) then
|
100 |
|
|
Move (Father, C);
|
101 |
|
|
C := Father;
|
102 |
|
|
else
|
103 |
|
|
exit;
|
104 |
|
|
end if;
|
105 |
|
|
end loop;
|
106 |
|
|
|
107 |
|
|
-- Last step is to pop the sifted node into place
|
108 |
|
|
|
109 |
|
|
Move (0, C);
|
110 |
|
|
end Sift;
|
111 |
|
|
|
112 |
|
|
-- Start of processing for Sort
|
113 |
|
|
|
114 |
|
|
begin
|
115 |
|
|
-- Phase one of heapsort is to build the heap. This is done by
|
116 |
|
|
-- sifting nodes N/2 .. 1 in sequence.
|
117 |
|
|
|
118 |
|
|
for J in reverse 1 .. N / 2 loop
|
119 |
|
|
Move (J, 0);
|
120 |
|
|
Sift (J);
|
121 |
|
|
end loop;
|
122 |
|
|
|
123 |
|
|
-- In phase 2, the largest node is moved to end, reducing the size
|
124 |
|
|
-- of the tree by one, and the displaced node is sifted down from
|
125 |
|
|
-- the top, so that the largest node is again at the top.
|
126 |
|
|
|
127 |
|
|
while Max > 1 loop
|
128 |
|
|
Move (Max, 0);
|
129 |
|
|
Move (1, Max);
|
130 |
|
|
Max := Max - 1;
|
131 |
|
|
Sift (1);
|
132 |
|
|
end loop;
|
133 |
|
|
|
134 |
|
|
end Sort;
|
135 |
|
|
|
136 |
|
|
end GNAT.Heap_Sort_A;
|