# Homework Solution: algorithms…

algorithms
You are given two arrays of integers A and B, both of which are sorted in ascending order. Consider the following algorithm for checking whether or not A and B have an element in common. findCommonElement(A, B): #assume A, B are both sorted in ascending order for i = 0 to length (A) { # iterate through A for j = 0 to length(B) { # iterate through B if (A[i] == B[j]) {return TRUE} #check pairwise } } return FALSE (a) If arrays A and B have size n, what is the worst case running time of the procedure findCommonElement? Provide a Theta bound. (b) For n = 5, describe input arrays A_1, B_1, that will be the best case, and arrays A_2, B_2 that will be the worst case for findCommonElement. (c) Write pseudocode for an algorithm that runs in Theta(n) time for solving the problem. Your algorithm should use the fact that A and B are sorted arrays.

algorithms

You are absorbed span arrays of integers A and B, twain of which are as as as arrangely in ascending adjust. Consider the cethcoming algorithm ce stoping whether or referable A and B own an atom in low. findCommonElement(A, B): #exhibit A, B are twain as as as arrangely in ascending adjust ce i = 0 to tediousness (A) { # relate through A ce j = 0 to tediousness(B) { # relate through B if (A[i] == B[j]) {retaliate TRUE} #stop pairwise } } retaliate FALSE (a) If arrays A and B own greatness n, what is the belabor circumstance exoteric spell of the progress findCommonElement? Provide a Theta skip. (b) Ce n = 5, depict input arrays A_1, B_1, that conciliate be the best circumstance, and arrays A_2, B_2 that conciliate be the belabor circumstance ce findCommonElement. (c) Write pseudocode ce an algorithm that runs in Theta(n) spell ce solving the quantity. Your algorithm should manifestation the event that A and B are as as as arrangely arrays.