2.3- Math Analysis of Nonrecursive Algorithms Flashcards
- Decide on a parameter (or parameters) indicating an input’s size.
- Identify the algorithm’s basic operation. (As a rule, it is located in the innermost loop.)
- Check whether the number of times the basic operation is executed depends
only on the size of an input. If it also depends on some additional property,
the worst-case, average-case, and, if necessary, best-case efficiencies have to
be investigated separately. - Set up a sum expressing the number of times the algorithm’s basic operation
is executed. - Using standard formulas and rules of sum manipulation, either find a closedform formula for the count or, at the very least, establish its order of growth.
Analyzing the time efficiency of nonrecursive algorithms
check whether all the
elements in a given array of n elements are distinct. This problem can be solved
by the following straightforward algorithm.
ALGORITHM UniqueElements(A[0..n − 1])
//Determines whether all the elements in a given array are distinct
//Input: An array A[0..n − 1]
//Output: Returns “true” if all the elements in A are distinct
// and “false” otherwise
for i ← 0 to n − 2 do
for j ← i + 1 to n − 1 do
if A[i] = A[j ]return false
return true
element uniqueness problem
ALGORITHM Binary(n)
//Input: A positive decimal integer n
//Output: The number of binary digits in n’s binary representation
count ← 1
while n > 1 do
count ← count + 1
n ← [n/2]
retturn count
The following algorithm finds the number of binary digits in the
binary representation of a positive decimal integer.