summaryrefslogtreecommitdiff
path: root/sort/odd_even.adb
blob: 095850e9efabe123dd1cb735566259cd1fba0fc3 (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
47
48
49
50
51
52
53
54
55


package body Odd_Even is


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


    procedure Single(Arr : in out Array_T) is
        Sorted : Boolean;
    begin
        if Arr'Length <= 1 then
            return;
        end if;

        loop
            Sorted := True;

            declare
                I : Integer := Index_T'Pos(Arr'First);
            begin
                while I < Index_T'Pos(Arr'Last) loop
                    if Arr(Index_T'Val(I)) > Arr(Index_T'Val(I+1)) then
                        Swap( Arr(Index_T'Val(I)), Arr(Index_T'Val(I+1)) );
                        Sorted := False;
                    end if;
                    I := I + 2;
                end loop;
            end;

            declare
                I : Integer := Index_T'Pos(Arr'First) + 1;
            begin
                while I < Index_T'Pos(Arr'Last) loop
                    if Arr(Index_T'Val(I)) > Arr(Index_T'Val(I+1)) then
                        Swap( Arr(Index_T'Val(I)), Arr(Index_T'Val(I+1)) );
                        Sorted := False;
                    end if;
                    I := I + 2;
                end loop;
            end;

            exit when Sorted;
        end loop;
    end Single;


end Odd_Even;