Ściaga sortowania, algorytmy i struktury danych


Bąbelkowe

for j:=1 to n do

for i:=1 to n do

begin

if A[i]>A[i+1] then

begin

pom:=A[i];

A[i]:=A[i+1];

A[i+1]:=pom;

end;

end;

Wybieranie

for j:=1 to n-1 do

begin

min:=j;

for i:= j+1 to n do

if A[i]<A[min] then min:= i;

k:=A[min];

A[min]:=A[j];

A[j]:=k;

end;

Wstawianie

for j:=n-1 downto 1 do

begin

k:=A[j];

i:=j+1;

while (i<=n) and (k>A[i]) do

begin

A[i-1]:=A[i];

i:=i+1;

end;

A[i-1]:=k;

end;

Scalanie

procedure PrzezScalanie(p,k:integer);

var s,l,m:integer;

begin

s:=(p+k+1) div 2;

if s-p>1 then PrzezScalanie(p,s-1);

if k-s>0 then PrzezScalanie(s,k);

l:=p;

m:=s;

for i:=p to k do

if (l=s) or ((m<=k) and (t1[l]>t1[m])) then

begin

t2[i]:=t1[m];

inc(m);

end

else

begin

t2[i]:=t1[l];

inc(l);

end;

for i:=p to k do t1[i]:=t2[i];

end;

Szybkie

procedure Quicksort(p,k:integer);

var

i,j,l,x:integer;

begin

i:=(p+k) div 2;

l:=t[i];

t[i]:=t[k];

j:=p;

for i:=p to k-1 do

if t[i]<l then

begin

x:=t[i];

t[i]:=t[j];

t[j]:=x;

inc(j);

end;

t[k]:=t[j];

t[j]:=l;

if p<j-1 then Quicksort(p,j-1);

if j+1<k then Quicksort(j+1,k);

end;

wst. Binarne

for j:=n-1 downto 1 do

begin

k:=t[j];

ip:=j;

ik:=S+1;

while ik-ip>1 do

begin

i:=(ip+ik) div 2;

if k<=t[i] then ik:=i else ip:=i;

end;

for i:=j to ip-1 do

t[i]:=t[i+1];

t[ip]:=k;

end;

procedure dnp(var p:wsk; d:integer);

var pom:wsk;

begin

new(pom);

pom^.dana:=d;

pom^.nastepny:=p;

p:=pom;

end;

procedure dnk(var p:wsk; d:integer);

var pom,pom1:wsk;

begin

if p=nil then

dnp(p,d)

else

begin

pom:=p;

while (pom^.nastepny<>nil) do

pom:=pom^.nastepny;

new(pom^.nastepny);

pom^.nastepny^.dana:=d;

pom^.nastepny^.nastepny:=nil;

end; end;

procedure uzp(var p:wsk);

var pom:wsk;

begin

if (p<>nil) then

begin

pom:=p;

p:=pom^.nastepny;

dispose(pom);

end

else exit;

end;

procedure uzk(var p:wsk);

var pom,pom1:wsk;

begin

if p=nil then exit

else

if p^.nastepny=nil then

begin

dispose(p);

p:=nil

end

else

begin

pom:=p;

while (pom^.nastepny<>nil) do

begin

pom1:=pom;

pom:=pom^.nastepny;

end;

dispose(pom);

pom1^.nastepny:=nil;

end;

end;



Wyszukiwarka