All pastes #533579 Raw Edit

Ada quicksort

public text v1 · immutable
·published 2007-06-03 14:42 UTC
rendered paste body
with ada.integer_text_io, ada.text_io;
use ada.integer_text_io, ada.text_io;

procedure quick is
        MAX : constant integer := 10;
        type llista is array(1..MAX) of integer;

        actual : llista := (30,1,2,3,45,12,32,44,10,5);

        procedure quicksort ( a : in out llista; inf : in integer; sup : in integer ) is
                div : integer := a(sup);
                i : integer := inf;
                j : integer := sup - 1;
                temp : integer;
                count : boolean := true;
                limits : exception;
        begin
                -- comprovam que no hi hagi problemes amb els indexs
                if inf >= sup then
                        raise limits;
                end if;

                while count loop
                        while a(i) < div loop
                                i := i + 1;
                        end loop;
                        while a(j) > div loop
                                j := j - 1;
                        end loop;
                        if i < j then
                                temp := a(i);
                                a(i) := a(j);
                                a(j) := temp;
                        else
                                count := false;
                        end if;
                end loop;

                temp := a(i);
                a(i) := a(sup);
                a(sup) := temp;

                quicksort(a,inf,i-1);
                quicksort(a,i+1,sup);

                exception
                        when limits => put_line("L'has liada un ou, l'index superior ha de ser major que l'inferior.");
        end quicksort;

        procedure mostra ( a : llista ) is
        begin
                for i in 1..MAX loop
                        put(a(i),4);
                end loop;
                new_line;
        end mostra;

begin

        mostra(actual);

        quicksort(actual,1,10);

        mostra(actual);

end quick;