Please use the file provided for coding..

Added on - 16 Sep 2019

  • 2


  • 959


  • 17


  • 0


Trusted by +2 million users,
1000+ happy students everyday
Showing pages 1 to 1 of 2 pages
Please use the file provided for coding.Note that the beginning of an array or list will have an index of 0. A file called isprovided containing classes. You should examine the classes so that you are aware of theattributes and methods available. Do not modify the classes unless asked to do so. Allprogramming problems will go in the files and Fill in the relevantfunction or method, do not change the parameters. The imports for these classes are contained atthe top. Do not use these names for variables.1. Assume that we have an array containing n elements and we will search the array somenumber of times in the future. Now consider two possible approaches to searching this array. The1st approach simply performs a linear search through the array each time. The second approachsorts the array prior to searching for any element after which a binary search will be used foreach search. Answer the following questions giving justification for your answer.a)Suppose that we perform O(lgn) searches. What are the worst case running times usingboth procedures? Which is better?b)Suppose that we perform Ω √n searches. What are the worst case running times of thetwo procedures? Which is better?c)If MergeSort is used for the sorting procedure what is the best case running time for thesecond approach?d)If InsertionSort is used for the sorting procedure what is the best case running time for thesecond approach?2. The following function determines whether or not a non-empty string S is a substring of sometarget string T. If it is the function returns True otherwise it returns False. The function works byconsidering every index for which T [index] == S[0] and index ≤ len(T) - en(S) as a possiblestart of a substring which matches S. The function begins at this character and proceeds checkingthe characters of T against S until either the entire string is matched or there is a miss at whichpoint it moves to the next index where T matches the first character of S. Answer the questionsbelow the following code. Note tm is an abbreviation for total matched. Below let n denote thelength of the string Tdef search (S ,T):first char = S[0]matches = []len (S)+for i in range (len(T) 1):if T[i] == first char:matches.append(i)for index in matches:tm = 0while tm < len (S) and T[index + tm] == S [tm]:tm += 1if tm == len (S):return Truereturn False
You’re reading a preview
Preview Documents

To View Complete Document

Click the button to download
Subscribe to our plans

Download This Document