DESCRIBING ALGORITHMS
- 1.
- Informal English description
- 2.
- Formal English description
- 3.
- Pseudo-Code
- 4.
- Code in programming lanuage (e.g., C++)
SORTING EXAMPLE for Array 1..n
I. Informal Description
- Find the smallest number and put it in the first position
- Find the second smallest number and put it in the second position
- Continue as above until the array is sorted
II. Formal Description
- 1.
- Set i := 1
- 2.
- Find the smallest number between positions i and n
- 3.
- Swap the number at i with the smallest number
- 4.
- Set i := i+1
- 5.
- if i < n go back to 2, otherwise quit
Pseudo-Code
procedure sort(intArray A[1..n])
for
pos := i
for
if
pos := j
fi
rof
swap(A[i], A[pos])
rof
end sort
EXPONENTIATION
INCREMENTAL
function exp(int a, int p) returns int
if
return 1
|
val := a
for
val := val*a
rof
fi
return val
end exp
EXPONENTIATION
DIVIDE-AND-CONQUER
function exp(int a, int p) returns int
if
return 1
|
val := exp(a, p/2)
if p is even
return val*val
| p is odd
return val*val*a
fi
fi
end exp
Bernard Waxman
8/26/1997