Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with all of the following please help with answering each part my tutor hasn't been too helpful thank you 3.1 Learning Objective:

I need help with all of the following

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

please help with answering each part my tutor hasn't been too helpful thank you

3.1 Learning Objective: To write a recursive method. Instructions: See the instructions in $1.1 for what to submit for grading. This is not a complete program. N class H03_31 and save it in a file named H0331.java. When you are done, copy H03_31.java to your asu folder, i.e., to the same folder as the PDF. Problem: The sum of the first n positive integers, n 21, is: sum(n) = Ei and can easily be computed using a for loop: public int sumitoN(int n) { // You may assumen 21 int sum = 0; for (int i = 1; i pList. String pkey, int pBeginIdx, int pEndIdx) that searches last element. Beiden to i meended for and return the indexanlinlist - A A Jly Hello world" then the method shall return "dlrow olleH". Hint: The base case occurs when the length of s is either 0 or 1. When the length is 0, it means s is the empty string! "" and the reverse of the empty string is the empty string. When the length is 1, it means that you have a string such as "A" and the reverse of "A" is "A" (or in general, the reverse of a string of length s 1 is the string itself). Otherwise, retrieve the first character of s at index 0. call the character c (hint: read about the String.charAt method). Extract the substring t at indices 1..s.length() - 1 (read the Java API documentation for the String.sub- string() method). Then, call reverselt) which will return a string, let's call the string revT. Concatenate c onto the end of revT and then return the newly formed string. Testing: We will be testing your method using our driver routine. For testing on your end, write your own test driver in a class named H03_35_Test. Within H03_35_Test.run() you should call reverse() on many different strings, in- cluding the empty string, to verify your solution is correct. 4 Linear and Binary Search 4.1 Learning Objective: To write a recursive method which searches a list for a key element. Instructions: See the instructions in $1.1 for what to submit for grading. This is not a complete program. Name your class H03_41 and save it in a file named H03_11.java. When you are done. copy HO3_41.java to your asuriteid-h03 folder, i.e., to the same folder as the PDF. Problem: We discussed in the lectures how to write a linear search algorithm using a for loop which iterates over each element of a list. Linear search can also be implemented recursively. For this exercise, write a recursive method public int recLinear Search ArrayList pList. String pkey. ant pBeginIdx. int pendId that searches pList elements pBeginidr up to and including pEndIdx for pkey and returns the index of prey in List in found or -1 if not found 3.1 Learning Objective: To write a recursive method. Instructions: See the instructions in $1.1 for what to submit for grading. This is not a complete program. Name your class H03_31 and save it in a file named H03_31.java. When you are done, copy H03_31.java to your asuriteid-h03 folder, 1.e., to the same folder as the PDF Problem: The sum of the first n positive integers, n 2 1. is: sum(n) = { and can easily be computed using a for loop: public int sumitoN(int n) { // You may ass int sum = 0; for (int i = 1; i liat = new ArrayList(); // we will populate liat vith several Stringa... int idx - TecLinear Search(liat, "the key", 0, list.size() - 1); Note that if list is empty, the method should return -1. Learning Objective: For the student to demonstrate that he or she understands how the recursive binary search algo rithm works. Instructions: This question is not graded. Problem: Suppose list is an ArrayList of Integers and contains these elements (note that list is sorted in ascending order): list = { 2, 3, 5, 10, 16, 24, 32, 48, 96, 120, 240, 360, 800, 1600 and we call the recursive binary search method, discussed in the lecture notes int index = recursiveBinarySearch(liat, 10, 0, list.size() - 1); where 10 is the key. O is the index of the first element in the range of list that we are searching this becomes pLot), and list.size() - 1 is the index of the last element in the range of list that we are searching this becomes pHigh). Trace the method by hand and show the following: (1) The values of pLow and pHigh on entry to each method call: (2) The value of middle that is computed: (3) State which clause of the if-elseif-elseif statement will be executed. ie. specify if return. middle; will be executed, or if return recursiveBinarySearch (plist, pkey, plow, middle 1): will be executed, or if return recursiveBinary Search (Liot, pkey, middle - 1. pHigh) will be executed (4) After the method returns, specify the value assigned to Index and the total number of times that recurate Bin list - {2, 3, 5, 10, 16, 24, 32, 48, 96, 120, 240, 360, 800, 1600 ) and we call the recursive binary search method, discussed in the lecture notes: int index = recursiveBinarySearch(list, 10, 0, list.size() - 1); where 10 is the key, 0 is the index of the first element in the range of list that we are searching this becomes plote). and list.size() - 1 is the index of the last element in the range of list that we are searching this becomes pHigh). Trace the method by hand and show the following: (1) The values of plow and pHigh on entry to each method call (2) The value of middle that is computed; (3) State which clause of the if-elseif elseif statement will be executed, ie. specify if return middle; will be executed, or if return recursiveBinarySearch(pList, pkey. PLow, siddle- 1); will be executed, or if return recursiveBinarySearch (pList, pkey, middle + 1, phigh); will be executed: (4) After the method returns, specify the value assigned to index and the total number of times that recursive Bin- arySearch) was called, including the original call shown above. 4.3 Repeat Exercise 42 but this time let pKey be 150. This exercise is graded, so include your trace in the word process ing document. 5 Analysis of Algorithms and Big O Notation 5.1 Learning Objective: To demonstrate that the student understands the definition of Big O. To demonstrate that the student knows how to use the definition of Big O to formally determine the asymptotic time complexity of a specific function Instructions: This exercise is graded, so include your solution in your word processing document. 5.2 Problem: Using the formal definition of Big O prove mathematically that fin) - 2.5n + 4 is on). Hint: you must choose values for C and 1 and a function in such that the criteria in definition are satisfied Learning Objective: To demonstrate that the student understands the definition of Big O. To demonstrate that the student knows how to use the definition of Big O to formally determine the asymptotic time complecity of a specific function. To demonstrate that the student understands that when An) is a constant, no matter how large, the time complexity is 01). Instructions: This exercise is not graded, so you do not need to include your solution in your word processing docu- ment. Problem: Using the formal definition of Big O prove mathematically that f(n) = 4x 10 is 0(1). Hint: you must determine values for C and 1 and a function g(n), such that the criteria in definition are satisfied. Learning Objective: To demonstrate that the student understands the definition of Big O. To demonstrate that the student understands that if a function f(n) is O(loga n) then that same function is also O(log.n), where a and b are constants. That is, different bases for the logarithm function do not change the time complexity. Instructions: This exercise is not graded, so you do not need to include your solution in your word processing docu- ment. Problem: An important concept to know regarding Big O notation is that for logarithmic complexity, the base is ir it. In other words, if fin) is a function that counts the number of times the key operation is performed as a function of n and An) is O(log. n) then it it also true that fin) is o(log. n). For example, binary search-which is usually stated as being O(l9 m)is also Olin n), O(logson), and O(loga.1418008571). Using the formal definition of Big O. prove mathematically that iffin) is O(log. n) then fin) is also O(log. n). Hint: First, show that log. 1 = (log. b)(log. n). Note that log. b) is a constant and that in the expression C 9n), C is a constant. Next, since we are trying to show that fin) - log.n is log, 1. note that you already showed that logan! = (loga b)(log. n). Learning Objective: To demonstrate the student can identify the key operation when finding the asymptotic time complexity of an algorithm. Instructions: This exercise is not graded, so you do not need to include your solution in your word processing docu- ment. Problem: Consider this splet) method where: pList is an ArrayList of Integer containing zero or more elements: pEvenList is an empty ArrayList of Integers and pOddist is an empty ArrayList of Integers. On return pEven ist will contain the even Integers of List and pOddist will contain the odd Integers pub toidsnyttet t hat -- is a constant. Next, since we are trying to show that (n) = log nis logs 7, note that you already showed that log. n - (loga b) (logo n). Learning Objective: To demonstrate the student can identify the key operation when finding the asymptotic time complexity of an algorithm. Instructions: This exercise is not graded, so you do not need to include your solution in your word processing docu- ment. Problem: Consider this split() method where: pList is an ArrayList of Integers containing zero or more elements; pEven List is an empty ArrayList of Integers, and pOddList is an empty ArrayList of Integers. On return, pEvenList will contain the even Integers of pL ist and pOddL ist will contain the od public void split(ArrayList PList, ArrayList pEvenList, ArrayList pList. Integer pkey. int PLow, int pHigh). int ternarySearch(ArrayList pList. Integer pkey) { int low = 0, high = pList.size() - 1; while (low pList.get(twoThirdIdx)) { low = twoThirdIdx + 1; } else { low = oneThirdIdx + 1; high = twoThirdldx - 1; 5.8 Testing: We will be testing your method using our testing driver. For testing on your end, write your own driver rou - tine in a class H03_57_Test. Note that if pList is empty, the method should return -1. You do not need to be con- cerned with plist being null Learning Objective: To demonstrate the student understands the definition of Big O. To informally prove the time complexity of an algorithm. Instructions: This exercise is not graded, so you do not need to include your solution in your word processing docu- ment. Problem: For rec Ternary Search() the key operations are the four comparisons of pKey to the the elements of pList. Treat these four comparisons as one comparison and provide an informal proof of the worst case time complexity of ternary search (doing so will not change the time complexity of the algorithm because it only multiplies g(n) by the constant 4). To simplify the analysis, assume that the size of the list on entry to rec TernarySearch() is always a power of 3, e.g., on the first call assume the size of the list is 3' on the second call the size of the list is 3 on the third call 37, and so on. 6 Sorting 6.1 Learning Objective: To demonstrate that the student can implement the java.lang. Comparable interface. Instructions: For this exercise you will be modifying the Point class declaration which was discussed in the Module 1 lecture notes in Objects and Classes : Section 1 (in cse205-note-objs-classes-01.pdf) The Point.java source code file can be found in the Module 1 Source Code zip archive). Include Point java in your assignment zip archive. Problem: Consider the Point class mentioned in the Instructions. Modify this class so it implements the java.lang. Comparable interface. We define Point pl to be less than Point p2 if the distance from the origin to p1 is less than the distance from the origin to pa pl is greater than p2 if the distance from the origin to pl is greater than the distance from the origin to p2: otherwise, if the distances are equal then pl is equal to p2. in ETA to search can be found in the Module 1 Source Code zip archive). Include Point.java in your assignment Zip archive. Problem: Consider the Point class mentioned in the Instructions. Modify this class so it implements the java.lang. Comparable

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions