| 1 |
------------------------------------------------------------------------------ |
| 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-2025, 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 3, 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. -- |
| 17 |
-- -- |
| 18 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 19 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 20 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 21 |
-- -- |
| 22 |
-- You should have received a copy of the GNU General Public License and -- |
| 23 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 24 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 25 |
-- <http://www.gnu.org/licenses/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
package body GNAT.Heap_Sort_A is |
| 33 |
|
| 34 |
---------- |
| 35 |
-- Sort -- |
| 36 |
---------- |
| 37 |
|
| 38 |
-- We are using the classical heapsort algorithm (i.e. Floyd's Treesort3) |
| 39 |
-- as described by Knuth ("The Art of Programming", Volume III, first |
| 40 |
-- edition, section 5.2.3, p. 145-147) with the modification that is |
| 41 |
-- mentioned in exercise 18. For more details on this algorithm, see |
| 42 |
-- Robert B. K. Dewar PhD thesis "The use of Computers in the X-ray |
| 43 |
-- Phase Problem". University of Chicago, 1968, which was the first |
| 44 |
-- publication of the modification, which reduces the number of compares |
| 45 |
-- from 2NlogN to NlogN. |
| 46 |
|
| 47 |
procedure Sort (N : Natural; Move : Move_Procedure; Lt : Lt_Function) is |
| 48 |
|
| 49 |
Max : Natural := N; |
| 50 |
-- Current Max index in tree being sifted |
| 51 |
|
| 52 |
procedure Sift (S : Positive); |
| 53 |
-- This procedure sifts up node S, i.e. converts the subtree rooted |
| 54 |
-- at node S into a heap, given the precondition that any sons of |
| 55 |
-- S are already heaps. On entry, the contents of node S is found |
| 56 |
-- in the temporary (index 0), the actual contents of node S on |
| 57 |
-- entry are irrelevant. This is just a minor optimization to avoid |
| 58 |
-- what would otherwise be two junk moves in phase two of the sort. |
| 59 |
|
| 60 |
procedure Sift (S : Positive) is |
| 61 |
C : Positive := S; |
| 62 |
Son : Positive; |
| 63 |
Father : Positive; |
| 64 |
|
| 65 |
begin |
| 66 |
-- This is where the optimization is done, normally we would do a |
| 67 |
-- comparison at each stage between the current node and the larger |
| 68 |
-- of the two sons, and continue the sift only if the current node |
| 69 |
-- was less than this maximum. In this modified optimized version, |
| 70 |
-- we assume that the current node will be less than the larger |
| 71 |
-- son, and unconditionally sift up. Then when we get to the bottom |
| 72 |
-- of the tree, we check parents to make sure that we did not make |
| 73 |
-- a mistake. This roughly cuts the number of comparisons in half, |
| 74 |
-- since it is almost always the case that our assumption is correct. |
| 75 |
|
| 76 |
-- Loop to pull up larger sons |
| 77 |
|
| 78 |
loop |
| 79 |
Son := 2 * C; |
| 80 |
exit when Son > Max; |
| 81 |
|
| 82 |
if Son < Max and then Lt (Son, Son + 1) then |
| 83 |
Son := Son + 1; |
| 84 |
end if; |
| 85 |
|
| 86 |
Move (Son, C); |
| 87 |
C := Son; |
| 88 |
end loop; |
| 89 |
|
| 90 |
-- Loop to check fathers |
| 91 |
|
| 92 |
while C /= S loop |
| 93 |
Father := C / 2; |
| 94 |
|
| 95 |
if Lt (Father, 0) then |
| 96 |
Move (Father, C); |
| 97 |
C := Father; |
| 98 |
else |
| 99 |
exit; |
| 100 |
end if; |
| 101 |
end loop; |
| 102 |
|
| 103 |
-- Last step is to pop the sifted node into place |
| 104 |
|
| 105 |
Move (0, C); |
| 106 |
end Sift; |
| 107 |
|
| 108 |
-- Start of processing for Sort |
| 109 |
|
| 110 |
begin |
| 111 |
-- Phase one of heapsort is to build the heap. This is done by |
| 112 |
-- sifting nodes N/2 .. 1 in sequence. |
| 113 |
|
| 114 |
for J in reverse 1 .. N / 2 loop |
| 115 |
Move (J, 0); |
| 116 |
Sift (J); |
| 117 |
end loop; |
| 118 |
|
| 119 |
-- In phase 2, the largest node is moved to end, reducing the size |
| 120 |
-- of the tree by one, and the displaced node is sifted down from |
| 121 |
-- the top, so that the largest node is again at the top. |
| 122 |
|
| 123 |
while Max > 1 loop |
| 124 |
Move (Max, 0); |
| 125 |
Move (1, Max); |
| 126 |
Max := Max - 1; |
| 127 |
Sift (1); |
| 128 |
end loop; |
| 129 |
|
| 130 |
end Sort; |
| 131 |
|
| 132 |
end GNAT.Heap_Sort_A; |