Ifi<=jthen
Begin
Temp:
=a[i];a[i]:
=a[j];a[j]:
=temp;
Inc(i);dec(j);
End;
Untili>j;
IfiIfbeEnd;
Procedurequicksort
Begin
Fori:
=1tondo
Read(a[i]);
Qsort(1,n);
End;
堆排序Heapsort
Procedureheapsort;
Begin
Heapn:
=0;
Fori:
=1tondo
Begin
read(x);
Insert(x);//**Heap
End;
Fori:
=1tondo
Delete
(1);//**Heap
End;
矩阵matrix
Procedurematrix;
Begin
Fori:
=1tondo
Forj:
=1tomdo
Fork:
=1totdo
C[I,j]:
=c[I,j]+a[I,k]*b[k,j];
End;
并查集Find_UnionSet
Functionfind(x:
longint):
longint;
Varvi,vj,sum:
longint;
Temp:
array[1..10000]oflongint;
Begin
Sum:
=0;
Inc(sum);
Temp[sum]:
=x;
Vi:
=x;
Whilex<>sset[x]do
Begin
Inc(sum);
Temp[sum]:
=sset[x];
X:
=sset[x];
End;
Forvj:
=1tosumdo
Sset[temp[vj]]:
=x;
Find:
=x;
End;
Procedurefind_unionset;
Begin
Iffind(x)=find(y)thenwriteln(‘YES’)
Else
Begin
X:
=find(x);y:
=find(y);
Sset[x]:
=sset[y];
Writeln(‘NO’);
End;
End;
树结构
排序二叉树BST
Procedureinsert(x:
longint);
Varp,q,s:
point;
Begin
New(p);p:
=root;new(q);q:
=nil;
New(s);s^.data:
=x;s^.left:
=nil;s^.right:
=nil;
Flag:
=true;
Whileflagand(p<>nil)do
Begin
Q:
=p;
Ifp^.data>s^.datathenp:
=p^.left
Else
Ifp^.data
=p^.right
Else
Begin
Flag:
=false;
Exit;
End;
End;
Ifs^.data>q^.datathenq^.right:
=selseq^.left:
=s;
Dispose(p);dispose(q);
End;
Procedurebst;
Begin
New(root);
Root^.left:
=nil;root^.right:
=nil;
Read(x);
Root^.data:
=x;
Fori:
=2ton-1do
Begin
Read(x);
Insert(x);
End;
End;
二叉堆Heap
Procedureinsert(x:
longint);
VarI,j:
longint;
Begin
Inc(heapn);
Heap[heapn]:
=x;
I:
=heapn;j:
=Idiv2;
While(j>0)and(heap[i]>heap[j])do
Begin
Temp:
=heap[i];heap[i]:
=heap[j];heap[j]:
=temp;
I:
=j;j:
=jdiv2;
End;
End;
Proceduredelete(k:
longint);
VarI,j:
longint;
Begin
Heap[k]:
=heap[heapn];
Dec(heapn);
I:
=k;j:
=k*2;
If(j+1<=heapn)and(heap[j]While(j<=heapn)and(heap[i]Begin
Temp:
=heap[i];heap[i]:
=heap[j];heap[j]:
=temp;
I:
=j;j:
=j*2;
If(j+1<=heapn)and(heap[j]End;
End;
Procedureheap;
Begin
Heapn:
=0;
fori:
=1tondo
begin
readln(x);
insert(x);
end;
fori:
=ndownto1do
delete(i);
end;
图论
最短路径Floyed
Procedurefloyed;
Begin
Fork:
=1tondo
Fori:
=1tondo
Forj:
=1tondo
If(i<>j)and(i<>k)and(k<>j)and(g[i,k]+g[k,j]G[I,j]:
=g[I,k]+g[k,j];
End;
最短路径Dijkstra
Proceduredijkstra;
Begin
Fork:
=1ton-1do
Begin
I:
=0;min:
=maxlongint;
Forj:
=1tondo
If(hash[j]=0)and(dis[j]Begin
Min:
=dis[j];
I:
=j;
End;
Ifi=0thenexit;
Hash[i]:
=1;
Forj:
=1tondo
If(hash[j]=0)and(dis[i]+g[I,j]Dis[j]:
=dis[i]+g[I,j];
End;
End;
最短路径Bellman_Ford
Procedurebellmanford;
Begin
Repeat
Flag:
=true;
Fori:
=1tomdo
Begin
X:
=line[i].x;y:
=line[i].y;w:
=line[i].w;
Ifdis[x]+wBegin
Dis[y]:
=dis[x]+w;
Flag:
=false;
End;
End;
Untilflag;
End;
最短路径SPFA
Procedurespfa;
Begin
Front:
=0;rear:
=1;
Q[1]:
=s;hash[s]:
=1;
Whilefront<>reardo
Begin
X:
=q[(front+1)modmaxn];
Ifstartv[x]<>0then
Fori:
=startv[x]toendv[x]do
Begin
Y:
=line[i].y;w:
=line[i].w;
Ifdis[x]>dis[y]+wthen
Begin
Dis[x]:
=dis[y]+w;
Ifhash[y]=0then
Begin
Hash[y]:
=1;
Rear:
=(rear+1)modmaxn;
Q[rear]:
=y;
End;
End;
End;
Front:
=(front+1)modmaxn;
Hash[x]:
=0;
End;
End;
MST问题Prim
Procedureprim;
Begin
fori:
=1ton-1do
begin
line[i].x:
=1;line[i].y:
=i+1;line[i].w:
=g[1,i+1];
end;
fork:
=1ton-1do
begin
min:
=maxlongint;i:
=0;
forj:
=ktondo
ifline[j].wbegin
min:
=line[j].w;i:
=j;
end;
ifi=0thenexit;
temp:
=line[i];line[i]:
=line[k];line[k]:
=temp;
x:
=line[k].y;
forj:
=k+1ton-1do
begin
y:
=line[j].y;w:
=g[x,y];
ifwbegin
line[i].w:
=w;
line[i].x:
=x;
end;
end;
end;
End;
MST问题Kruscal
Procedurekruscal;
Begin
Qsort(1,m);//**Quicksort
Sum:
=0;count:
=0;
Forj:
=1tomdo
Begin
X:
=find(line[i].x);y:
=find(line[i].y);
Ifx<>ythen
Begin
Sset[x]:
=sset[y];//**Find_UnionSet
Inc(count,line[i].w);
Inc(sum);
Ifsum=n-1thenbreak;
End;
End;
End;
Euler图
Procedurefind(x:
longint);
VarI:
longint;
Begin
Ifoutdegree[x]=0then
Begin
Inc(sum);
Cnt[sum]:
=x;
End
Else
Begin
Fori:
=1tondo
Ifg[x,i]<>0then
Begin
G[x,i]:
=0;
Dec(outdegree[x]);
Find(i);
End;
Inc(sum);
Cnt[sum]:
=x;
End;
End;
Procedureeuler;
Begin
Sum:
=0;
Fori:
=1tondo
Ifodd(outdegree[i])thenbeginfind(i);exit;end;
Find
(1);
End;
AOV网(topo_sort)
Proceduretopo_sort;
Begin
Front:
=1;rear:
=0;
fori:
=1tondo
Ifindegree[i]=0then
Begin
Inc(rear);
Q[rear]:
=I;
End;
Whilefront<=reardo
Begin
X:
=q[front];
Fori:
=1tondo
Ifg[x,i]<>0then
Begin
Dec(indegree[i]);
Ifindegree[i]=0then
Begin
Inc(rear);
Q[rear]:
=I;
End;
End’
Inc(front);
End;
End;
AOE网
ProcedureAOE;
Begin
line1:
=line;
front:
=1;rear:
=0;
fori:
=1tondo
ifindegree[i]=0then
begin
inc(rear);
q[rear]:
=i;
early[i]:
=0;
end;
whilefront<=reardo
begin
x:
=q[front];
ifstartv[x]<>0then
fori:
=startv[x]toendv[x]do
begin
y:
=line[i].y;
w:
=line[i].w;
dec(indeg[y]);
ifindeg[y]=0then
begin
inc(rear);
q[rear]:
=y;
end;
ifearly[y]early[y]:
=early[x]+w;
end;
inc(front);
end;
line:
=line1;
fori:
=1tondo
last[i]:
=maxint;
last[q[rear]]:
=early[q[rear]];
fori:
=rear-1downto1do
begin
x:
=q[i];
ifstartv[x]<>0then
forj:
=startv[x]toendv[x]do
iflast[x]>last[line[j].y]-line[j].wthen
last[x]:
=last[line[j].y]-line[j].w;
end;
end;
网络流Ford_Fulkson
Functionfindpath:
boolean;
vari,x:
longint;
begin
can:
=0;
hash[s]:
=maxint;
q[1]:
=s;front:
=1;rear:
=1;
whilefront<=reardo
begin
x:
=q[front];
fori:
=1tondo
if(hash[i]=0)and(c[x,i]-f[x,i]>0)and(min(hash[x],c[x,i]-f[x,i])>hash[i])then
begin
in