1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . B U B B L E _ S O R T --
9 -- Copyright (C) 1995-2010, AdaCore --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- Sort Utility (Using Bubblesort Algorithm)
34 -- This package provides a bubblesort routine that works with access to
35 -- subprogram parameters, so that it can be used with different types with
36 -- shared sorting code.
38 -- See also GNAT.Bubble_Sort_G and GNAT.Bubble_Sort_A. These are older
39 -- versions of this routine. In some cases GNAT.Bubble_Sort_G may be a
40 -- little faster than GNAT.Bubble_Sort, at the expense of generic code
41 -- duplication and a less convenient interface. The generic version also
42 -- has the advantage of being Pure, while this unit can only be Preelaborate.
44 package GNAT
.Bubble_Sort
is
47 -- The data to be sorted is assumed to be indexed by integer values from
48 -- 1 to N, where N is the number of items to be sorted.
50 type Xchg_Procedure
is access procedure (Op1
, Op2
: Natural);
51 -- A pointer to a procedure that exchanges the two data items whose
52 -- index values are Op1 and Op2.
54 type Lt_Function
is access function (Op1
, Op2
: Natural) return Boolean;
55 -- A pointer to a function that compares two items and returns True if
56 -- the item with index value Op1 is less than the item with Index value
57 -- Op2, and False if the Op1 item is greater than or equal to the Op2
60 procedure Sort
(N
: Natural; Xchg
: Xchg_Procedure
; Lt
: Lt_Function
);
61 -- This procedures sorts items in the range from 1 to N into ascending
62 -- order making calls to Lt to do required comparisons, and calls to
63 -- Xchg to exchange items. The sort is stable, that is the order of
64 -- equal items in the input is preserved.