Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

5.7 Learning Objective: To demonstrate that the student understands how the binary search algorithm works and to implement an algorithm that is similar to binary

5.7 Learning Objective: To demonstrate that the student understands how the binary search algorithm works and to implement an algorithm that is similar to binary search. Instructions: See the instructions in 1.1 for what to submit for grading. This is not a complete program. Name your class H03_57 and save it in a file named H03_57.java. When you are done, copy H03_57.java to your asuriteid-h03 folder, i.e., to the same folder as the PDF. Problem: Binary search is such an efficient searching algorithm because during each pass of the loop (in the iterative version) or in each method call (in the recursive version) the size of the list is essentially halved. An algorithm which repeatedly divides the problem size by two each time will have O(lg n) performance. If reducing the size of the list to be searched by two is so efficient, it may seem that dividing the problem size by three each time would be even faster. To that end, consider this iterative ternary (ternary is base three) search method. Rewrite it is as a recursive ternary search method named int recTernarySearch(ArrayList pList, Integer pKey, int pLow, int pHigh). int ternarySearch(ArrayList pList, Integer pKey) { int low = 0, high = pList.size() - 1; while (low <= high) { int range = high - low; int oneThirdIdx = (int)Math.round(low + range / 3.0); int twoThirdIdx = (int)Math.round(low + range / 1.3333333333333333); if (pKey.equals(pList.get(oneThirdIdx))) { return oneThirdIdx;} else if (pKey.equals(pList.get(twoThirdIdx))) { return twoThirdIdx; } else if (pKey < pList.get(oneThirdIdx)) { high = oneThirdIdx - 1; } else if (pKey > pList.get(twoThirdIdx)) { low = twoThirdIdx + 1; } else { low = oneThirdIdx + 1; high = twoThirdIdx - 1; } } return -1; } Testing: We will be testing your method using our driver routine. For testing on your end, write your own driver routine in a class different than H03_57. Note that if pList is empty, the method should return -1.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Question

someexamples of the "Reporting Net vs. Gross Revenue"

Answered: 1 week ago

Question

Develop clear policy statements.

Answered: 1 week ago

Question

Draft a business plan.

Answered: 1 week ago

Question

Describe the guidelines for appropriate use of the direct plan.

Answered: 1 week ago