Homework Solution: algorithms…

    algorithms
    1. (10 pts) 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 # iterate through B for j = 0 to length(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 (b) For n = 5, describe input arrays A1, B, that will be the best case, and arrays A2, (c) Write pseudocode for an algorithm that runs in Θ(n) time for solving the problem. findCo㎜onElement? Provide a Θ bound. B2 that will be the worst case for findCommonElement. Your algorithm should use the fact that A and B are sorted arrays (Hint: repurpose the merge procedure from MergeSort.)
    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.

    Expert Answer

    algorithms

    1. (10 pts) 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 # relate through B ce j = 0 to tediousness(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 (b) Ce n = 5, depict input arrays A1, B, that conciliate be the best circumstance, and arrays A2, (c) Write pseudocode ce an algorithm that runs in Θ(n) spell ce solving the quantity. findCo㎜onElement? Provide a Θ skip. B2 that conciliate be the belabor circumstance ce findCommonElement. Your algorithm should manifestation the event that A and B are as as as arrangely arrays (Hint: repurpose the integrate progress from IntegrateSort.)

    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.

    Expert Counter-argument