Question

S19-A6.pdf 2/5 Rutgers University CS111 - Inlroduction to Computer Science Spring 2019 Problem 5 Big-oh For cach problen give

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a.

bool common(A1, m, A2, n)

1. For i= 1 to m:-

2.......For j=1 to n

3.............If A[i] == A[j]

4....................Return True //since they have common element

5. Return False //No element found in common

Time complexity = O(mn) in all the cases in total number of comparisons will be m*n. We can reduce this time complexity by sorting array A1 and A2 which takes time O(n log n +m log m) and then comparison for finding duplicate entry will take O(n+m).

b.

int count_duplicate(string s, int n)

1. count= 0

2. Bool checked[256]=false //this will be true if ith character in ASCII has been checked

3. For i = 1 to n-1

4........... if (s[i] ==true) then continue //since this character has already been checked for duplicate

5............For j= i+1 to n

6..................If(s[i] == s[j] and checked[ s[i] ]== false)

7...........................checked[ s[i] ]= true and count= count+2

8..................Else if(s[i]==s[j] and checked[ s[i] ]==true)

9.......................... count++; //increment count for all duplicate

10 return count //desired result

Here best case will occur if entire string will have same element , in which case time complexity will be O(n) because innerloop will executes only during first iteration of outermost for loop.

Worst case will occur when entire string have different characters where time complexity will be O(n2).

c.

int find_row(A[][], int m, int n, x)

1. For i= 1 to m

2........For j=1 to n

3............. if (A[i][j] !=x)

4...................then continue //since row i do not have all x

5.............. if (j==n) //this means entire row i contain x

6..................then return(i)//since row i has all x

7. Return 0 //if no row have all x

Best case will occur when first entry of every row have value different from x, then time complexity will be O(n)

Worst case will be occur when in each row, every entry is x except the last entry. Then time complexity will be O(n2).

Please comment for any clarification.  

Add a comment
Know the answer?
Add Answer to:
S19-A6.pdf 2/5 Rutgers University CS111 - Inlroduction to Computer Science Spring 2019 Problem 5 Big-oh For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Searching/sorting tasks and efficiency analysis - Big-oh For each problem given below, do the following: 1....

    Searching/sorting tasks and efficiency analysis - Big-oh For each problem given below, do the following: 1. Create an algorithm in pseudocode to solve the problem. 2. Identify the factors that would influence the running time of your algorithm. For example, if your algorithm is to search an array the factor that influences the running time is the array size. Assign names (such as n) to each factor. 3. Count the operations performed by the algorithm. Express the count as a...

  • Answer B java Problem 2 For each problem given below, do the following: 1. Create an...

    Answer B java Problem 2 For each problem given below, do the following: 1. Create an algorithm in pseudocode to solve the problem. 2.Identify the factors that would influence the running time of your algorithm. For example, if your algorithm is to search an array the factor that influences the running time is the array size. Assign names (such as n) to each factor. 3. Determine the number of operations in each step of the pseudocode. To do that, identify...

  • For each problems segment given below, do the following: Create an algorithm to solve the problem...

    For each problems segment given below, do the following: Create an algorithm to solve the problem 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. 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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT