For each problems segment given below, do the following: Create an algorithm to
ID: 3559961 • Letter: F
Question
For each problems segment given below, do the following:
Create an algorithm to solve the problem (describe it in English as part of your answer).
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.
Identify the operations that must be counted.
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).
Determine what the Best Case Inputs are, and the Worst Case Inputs are.
Transform your count formula into big-O notation by:
Taking the efficiency with worst case input,
Dropping insignificant terms.
Dropping constant coefficients.
Problem 1: Determine if 2 arrays contain the same elements
Problem 2: Counting total number of characters that have a duplicate within a string (i.e. "gigi the gato" would result in 3, since g, i, and t are repeated.)
Problem 3: Finding an empty row in a 2-D array where empty is defined as a row with all entries equal to 0.
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 affect 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.