Write a program to implement binary search. Partial program is given as below: class BinarySearch {...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Write a program to implement binary search. Partial program is given as below: class BinarySearch { private static int binarySearch (int arr [], int target) { } int low = 0; int high arr.length - 1; int i=0; int middleIndexNumber = arr [middleIndex]; /*Print out information. You should print out the order of the loop (e.g., "This is loop 1"), the index of the midpoint of each loop, and the number of the midpoint of each loop. */ while (low <= high) { int middleIndex = i++; } } if (target == middleIndexNumber) { return } if } if } (target < middle IndexNumber) { high = cl (target > middle IndexNumber) { low = 3 return -1; public static void main(String args[]) { int [] NumerArray = (2,6,7,9,12, 18, 22, 25, 27, 29); System.out.println ("The index of the targeted number is " +binarySearch (NumerArray, 27)+"\n"); } If you run your code, the result should look like: <terminated> BinarySearch [Java Application] C:\Program Files\Eclipse Foundation\t This is loop 1 The index of midPoint is 4 The number in the midPoint is 12 This is loop 2 The index of midPoint is 7 The number in the midPoint is 25 This is loop 3 The index of midPoint is 8. The number in the midPoint is 27 The index of the targeted number is 8 Write a program to implement binary search. Partial program is given as below: class BinarySearch { private static int binarySearch (int arr [], int target) { } int low = 0; int high arr.length - 1; int i=0; int middleIndexNumber = arr [middleIndex]; /*Print out information. You should print out the order of the loop (e.g., "This is loop 1"), the index of the midpoint of each loop, and the number of the midpoint of each loop. */ while (low <= high) { int middleIndex = i++; } } if (target == middleIndexNumber) { return } if } if } (target < middle IndexNumber) { high = cl (target > middle IndexNumber) { low = 3 return -1; public static void main(String args[]) { int [] NumerArray = (2,6,7,9,12, 18, 22, 25, 27, 29); System.out.println ("The index of the targeted number is " +binarySearch (NumerArray, 27)+"\n"); } If you run your code, the result should look like: <terminated> BinarySearch [Java Application] C:\Program Files\Eclipse Foundation\t This is loop 1 The index of midPoint is 4 The number in the midPoint is 12 This is loop 2 The index of midPoint is 7 The number in the midPoint is 25 This is loop 3 The index of midPoint is 8. The number in the midPoint is 27 The index of the targeted number is 8
Expert Answer:
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Posted Date:
Students also viewed these programming questions
-
Let's consider a football sled of mass 200kg. When coach blows his whistle, a lineman applies a 2500N force for 5s. Assuming nothing stops the sled or slows it down, what would its final velocity be...
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
How can staff review the effectiveness of their work, the services they provide and the social and cultural factors impacting on clients, groups or communities?
-
Molybdenum (Mo) has a BCC crystal structure, an atomic radius of 0.1363 nm, and an atomic weight of 95.94 g/mol. Compute and compare its theoretical density with the experimental value found inside...
-
a. Sunshine Seashore Supplies rents beach equipment such as kayaks, canoes, beach chairs, and umbrellas to tourists. Write a program that displays Sunshines motto, which is Sunshine Seashore makes it...
-
Cristal Haymeyer, CPA, pays her new staff accountant, Anika, a salary equivalent to \($25\) per hour, while Cristal receives a salary equivalent to \($40\) per hour. The firms predetermined indirect...
-
Presented below are two independent situations. (a) On March 3, Kitselman Appliances sells $650,000 of its receivables to Ervay Factors Inc. Ervay Factors assesses a finance charge of 3% of the...
-
(a) NP and Co. has imported goods for US $ 7,00,000. The amount is payable after three months. The company has also exported goods for US $ 4,50,000 and this amount is receivable in two months. For...
-
Change all of the numbers in the data area of your worksheet so that it looks like this: A 1 Chapter 3: Applying Excel 2 3 Data 4 Allocation base 5 Estimated manufacturing overhead cost 6 Estimated...
-
Consider f(x) = cos(x+ 2) + 1; -2
-
1. Describe the project control process. Discuss how it can be applied to a project on which you are currently working or one you have recently worked on. 2. Why should a project have a well-defined...
-
Assume that you have invested 60% of your $30,000 in stocks and the rest in bonds. One year later you observe that your stocks rose by 5% while your bonds declined by 3%. How can you restore your...
-
How does your organization compare to GE or Johnson & Johnson regarding the strategic initiatives used to improve organizational processes? Use specific examples from the article provided, if...
-
GCC Stock latest dividend of AED 2.25 a share was paid yesterday. You plan to purchase the stock today because you believe the dividend shall grow @ 10% annually for next FOUR years, and selling...
-
What is the importance of team-building and state different techniques to build an effective team? How can you use the team building skills to assist you in your learning while studying and in your...
-
Mrs. Brown is 58 years of age, and her 2021 income is made up of a net employment income of $68,000: eligible dividends of $10,000. For 2021 her employer withheld CPP of $2,300; EI of $810 and...
-
Fill in each blank so that the resulting statement is true. A solution to a system of linear equations in two variables is an ordered pair that__________ .
-
There are several techniques for implementing the sqrt method in the Math class. One such technique is known as the Babylonian method. It approximates the square root of a number, n, by repeatedly...
-
Write a program that finds all occurrences of a word in all the files under a directory, recursively. Pass the parameters from the command line as follows: java Exercise18_30 dirName word
-
Write a simulation program to show that the chance of getting dead-end paths increases as the grid size increases. Your program simulates lattices with size from 10 to 80. For each lattice size,...
-
How does each of the following variables behave over the business cycle? Develop graphs to show your results and give economic explanations. a. Real imports b. Federal government receipts c. Housing...
-
Graph the levels of real GDP for the United States, Canada, and Germany (data can be found at www.oecd.org under Statistics and then under National Accounts). Are U.S. and Canadian business cycles...
-
Which school of economists, classical or Keynesian, supports government intervention in the face of economic shock? Explain your answer based on the underlying assumptions.
Study smarter with the SolutionInn App