Content deleted Content added
Nils Grimsmo (talk | contribs) m Insertion sort --> bubble sort. |
Nils Grimsmo (talk | contribs) Added Floyd-Warshall. Various fixes. |
||
Line 1:
The programming language UNITY was constructed by K. Mani Chandy and Jayadev Misra for their book ''Parallel Program Design: A Foundation''. It is a rather theoretical language, which tries to
All statements are assignments, and are separated by <code>#</code>. A statement can consist of multiple assignments, on the form <code>a,b,c := x,y,z</code>, or <code>a := x || b := y || c := z</code>. You can also have a ''quantified statement list'',
Line 8:
===Bubble sort===
[[Bubble sort]] the array by comparing adjacent numbers, and swapping them if they are in the wrong order. Using <math>\Theta(n)</math> expected time, <math>\Theta(n)</math> processors and <math>\Theta(n^2)</math>
Program
declare
n: integer,
Line 20:
<# k : 0 <= k < 2 ::
<|| i : i % 2 = k and 0 <= i < n - 1 ::
A[i], A[i+1] := A[i+1], A[i]
if A[i] > A[i+1] > > end
===Merge sort===
[[Merge sort]] the array using a constant time merge. Using <math>\Theta(\log n)</math> time
Program
declare
n,m: integer,
A,S,N: array [0..n-1] of integer
Line 34 ⟶ 35:
n = 20 #
m = n ||
<|| i : 0 <=
assign
<|| s : 0 <= s < m - 1 and s % 2 = 0 ::
Line 40 ⟶ 41:
<|| sa : 0 <= sa < N[s+a] ::
A[S[s]+sa] := A[S[s+a]+sa]
if a = 0 and A[S[s+a]+sa] <= A[S[s+b]]
or a = 1 and A[S[s+a]+sa] < A[S[s+b]] ||
<|| sb : 0 < sb < N[s+b] ::
A[S[s]+sa+sb] := A[S[s+a]+sa]
if a = 0 and A[S[s+b]+sb-1] <
or a = 1 and A[S
if a = 0 and A[S[s+b]+N[s+b]-1] < A[S[s+a]+sa]
or a = 1 and A[S[s+b]+N[s+b]-1] <= A[S[s+a]+sa] > > ||
S[s/2] := S[s] ||
N[s/2] := N[s] + N[s+1] > ||
S[(m-1)/2], N[(m-1)/2] := S[m-1], N[m-1] if m%2 = 1 ||
m := m/2 + m%2
end
===Floyd-Warshall===
Using the [[Floyd-Warshall_algorithm|Floyd-Warshall]] all pairs [[Shortest path problem|shortest path]] algorithm, we include intermediate nodes iteratively, and get <math>\Theta(n)</math> time, using <math>\Theta(n^2)</math> processors and <math>\Theta(n^3)</math> work.
Program shortestpath
declare
n,k: integer,
D: array [0..n-1, 0..n-1] of integer
initially
n = 10 #
k = 0 #
<|| i,j : 0 <= i < n and 0 <= j < n ::
D[i,j] = rand() % 100 >
assign
<|| i,j : 0 <= i < n and 0 <= j < n ::
D[i,j] := min(D[i,j], D[i,k] + D[k,j]) > ||
k := k + 1 if k < n - 1
end
|