summaryrefslogtreecommitdiff
path: root/sort/comb.adb
blob: d13847caff40667e71908f37c94c9f4332b04700 (plain)
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


package body Comb is


    procedure Swap(A, B : in out Element_T) is
        Temp : Element_T;
    begin
        Temp := A;
        A := B;
        B := Temp;
    end Swap;


    procedure Sort(Arr : in out Array_T) is
        Swapped : Boolean;
        Gap : Natural;

        Shrink : constant Float := 1.3;
    begin
        if Arr'Length <= 1 then
            return;
        end if;

        Gap := Arr'Length;

        loop
            Gap := Natural(Float'Floor(Float(Gap) / Shrink));
            if Gap < 1 then
                Gap := 1;
            end if;

            Swapped := False;
            for I in Integer range Index_T'Pos(Arr'First) .. (Index_T'Pos(Arr'Last) - Gap) loop
                if Arr(Index_T'Val(I)) > Arr(Index_T'Val(I + Gap)) then
                    Swap( Arr(Index_T'Val(I)), Arr(Index_T'Val(I + Gap)) );
                    Swapped := True;
                end if;
            end loop;
            exit when Gap = 1 and not Swapped;
        end loop;
    end Sort;


end Comb;