Algoritmos de ordenación (y van 4).
Por fin he sacado un algoritmo en claro.
Creo que el tiempo es siempre 1/2(n^2-n) y fuerza mucho con la recursividad.
No es n log n, pero me ha parecido bonito. No sé si funcionará porque no consigo probarlo.
Si sabéis si hay alguno parecido avisadme.
—————————————————————-
procedure ordenar (T: in out tpLista)is
—————————————————————-
————————————————————-
procedure OrdenarIzda (T: in out Tplista)is
————————————————————-
aux:tpDato;
begin
if Numdatos(T)>1 then
if T(T’First)>T(T’Last) then–intercambia
Aux:=T(T’First);
T(T’First):=T(T’Last);
T(T´Last):=Aux;
end if;
if T’Last-T’First/=1 then
OrdenarIzda(T(T’First..T’Last-1));
end if;
end if;
end Ordenarizda;
————————————————————-
————————————————————-
procedure OrdenarDcha (T: in out Tplista)is
————————————————————-
aux:tpDato;
begin
if Numdatos(T)>1 then
if T(T’First)>T(T’Last) then–intercambia
Aux:=T(T’First);
T(T’First):=T(T’Last);
T(T´Last):=Aux;
end if;
if T’Last-T’First/=1 then
OrdenarDcha(T(T’First+1..T’Last));
end if;
end if;
end OrdenarDcha;
————————————————————-
aux:tpDato;
begin
if Numdatos(T)>1 then
if T(T’First)>T(T’Last) then–intercambia
Aux:=T(T’First);
T(T’First):=T(T’Last);
T(T´Last):=Aux;
end if;
if T’Last-T’First/=1 then
OrdenarIzda(T(T’First..T’Last-1));
Ordenar(T(T’First+1..T’Last-1));
OrdenarDcha(T(T’First+1..T’Last));
end if;
end if;
end ordenar;
—————————————————————
April 17th, 2007 at 23:14
Me respondo yo a mi mismo.
Al algoritmo se le llama cocktail sort.
http://en.wikipedia.org/wiki/Selection_sort (al final de la página.