# CS 601_Mid-Term 2.docx - 1. Suppose you are given an array...

• 13

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 - 3 out of 13 pages.

1.Suppose you are given an array a [1, …, n] of distinct sorted (indecreasing order) integers that has been circularly shifted xpositions to the right. For example, [10, 5, 3, 99, 55, 47, 38, 25, 12]is a sorted array that has been circularly shifted x = 3 positions,while [5, 3, 99, 55, 47, 38, 25, 12, 10] has been shifted x = 2positions.1)Write a?(n) algorithm to find the largest element in the array.Justify why your algorithm is?(n). Provide enough details.2)Can you improve your algorithm in part 1 to something moreefficient? If yes, write the algorithm and analyze that in detail.If not, argue why?(n) is the most efficient algorithm you coulddesign for this question.Implement your most-efficient algorithm using ONLY C++/Java. Haveyour complete program files submitted together with your solutionfile. You need to read the array values from an input file calledinput.txt. Here is the required format:91053995547382512where 9 is the size of the array. The rest of numbers are the arrayvalues, i.e., a = [10, 5, 3, 99, 55, 47, 38, 25, 12].
Let I, j = 0For (int i; i<n; i++): n being the number of elements in array a{If a[i] > a[j]{J = I}}Return a[j]Time Complexity for above Algorithm:All the elements are traversed and compared linearly in thisalgorithm.This gives us the linear search algorithm with time complexity ofO (n).The for loop would execute n+1 number of times.The condition i++ would be executed n number of times.If condition would be checked n number of times.Rest all the operations would give us constant time execution O(1).Summing up n + 1 + n + n = 3n + 1. Ignoring the constants inthe result would yield us 3n.This would make the complexity of C*n = 3 * n = O (n)Hence time complexity for the above algorithm would beO (n).2.Algorithm to find largest element in a sorted list with better approach:1)A divide and conquer solution can be used to answer thisquestion in log n time.2)It would help us to split the array into two parts and only onepart of the array would have to be traversed to find the answer.

Course Hero member to access this document

Course Hero member to access this document

End of preview. Want to read all 13 pages?

Course Hero member to access this document

Term
Fall
Professor
NoProfessor
Tags
Shortest path problem, Dijkstra