For each problems segment given below, do the following: 1. Create an algorithm
ID: 3568722 • Letter: F
Question
For each problems segment given below, do the following:
1.Create an algorithm to solve the problem.
2. Identify the factors that would influence the running time, and which can be known before the algorithm or code is executed. Assign names (such as n) to each factor.
3. Identify the operations that must be counted. You need not count every statement separately. If a group of statements always executes together, treat the group as a single unit. If a method is called, and you do not know the running time of that method, count it as a single operation.
4.Count the operations performed by the algorithm or code. Express the count as a function of the factors you identified in Step 2. If the count cannot be expressed as a simple function of those factors, define the bounds that can be placed on the count: the best case (lower bound) and worst case (upper bound).
5.Determine what the Best Case Inputs are, and the Worst Case Inputs are, and the efficiency of your implementation Transform your count formula into big-O notation by:
Taking the efficiency with worst case input,
Dropping insignificant terms.
Dropping constant coefficients.
a. Determine if 2 arrays contain the same elements
b. Counting total number characters that have a duplicate within a string (i.e. "gigi the gato" would result in 7 (g x 3 + i x 2 + t x 2)
c. Finding an empty row in a 2-D array where empty is defined as an element with a 0 entry.
Explanation / Answer
a.
Algorithm SameOrNot( A,B)
The factor that effects the running time is the length of the arrays.
Counting operations:
Count of each statement
n=length(n)
1
if n != length(B)
1
return false
1
for i =1 to n
n+1
flag = false
1*n=n
for j =1 to n
n*(n+1)=n2
if A[i]==B[j]
n*n=n2
flag=true
0 or 1
If flag != true
n
return false
1
Total count
2n2+3n+6
Best and worst case inputs:
Big-O notation:
Count =2n2+3n+6 = O(n2) [ remove insignificant and constant coefficients]
b.
Here consider the string as a character array. Sort the given string and then compare side by side characters for finding count of duplicates.
Algorithm countDuplicates(str)
The factor that effects the running time is the length of the string.
Counting operations:
Count of each statement
n=length(str)
1
for i=1 to n
n+1
for j=1 to n
n(n+1)= n2+n
if str(i)>str(j)
n2
ch=str(i)
n2
str(i)=str(j)
n2
str(j)=ch
n2
dup_count=0
1
equal=false
1
for i =1 to n-1
n
if str(i)==str(i+1)
n-1
dup_count= dup_count +1
n-1
If equal==false
n-1
dup_count= dup_count +1
n-1
equal=true
n-1
else
equal=false
n-1
return dup_count
1
Total count
5n2 +9n-1
Best and worst case inputs:
Big-O notation:
Count =5n2 +9n-1= O(n2) [ remove insignificant and constant coefficients]
c.
Algorithm hasEmpty( 2D,m,n)
The factors that effect the running time are the dimensions (mXn) of the two dimensional arrays.
Counting operations:
Count of each statement
for i =1 to m
m+1
flag=false
m
for j =1 to n
n+1
if 2D[i , j] != 0
m*n
flag=true
m*n=n
If flag == false
m
return true
1
return false
1
Total count
2m*n+3m+n+4
Best and worst case inputs:
Big-O notation:
Count =2m*n+3m+n+4= O(m*n) [ remove insignificant and constant coefficients]
Count of each statement
n=length(n)
1
if n != length(B)
1
return false
1
for i =1 to n
n+1
flag = false
1*n=n
for j =1 to n
n*(n+1)=n2
if A[i]==B[j]
n*n=n2
flag=true
0 or 1
If flag != true
n
return false
1
Total count
2n2+3n+6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.