
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Question
Write the full Java code for LabProgram.java

Transcribed Image Text:12.18 LAB: Binary search
Binary search can be implemented as a recursive algorithm. Each call makes a recursive call on one-half of the list the call received as an
argument.
Complete the recursive method binarySearch() with the following specifications:
1. Parameters:
。 a target integer
。 an ArrayList of integers
。 lower and upper bounds within which the recursive call will search
2. Return value:
。 the index within the ArrayList where the target is located
。 -1 if target is not found
The template provides main() and a helper function that reads an ArrayList from input.
The algorithm begins by choosing an index midway between the lower and upper bounds.
1. If target ==
integers.get(index) return index
2. If lower == upper, return -1 to indicate not found
3. Otherwise call the function recursively on half the ArrayList parameter:
。 If integers.get(index) < target, search the ArrayList from index + 1 to upper
。 If integers.get(index) > target, search the ArrayList from lower to index - 1
The ArrayList must be ordered, but duplicates are allowed.
Once the search algorithm works correctly, add the following to binarySearch():
4. Count the number of calls to binarySearch().
5. Count the number of times when the target is compared to an element of the ArrayList. Note: lower
Hint: Use a static variable to count calls and comparisons.
The input of the program consists of:
1. the number of integers in the ArrayList
2. the integers in the ArrayList
3. the target to be located
Ex: If the input is:
== upper should not be counted.
9
1 2 3 4 56789
2
the output is:
index: 1, recursions: 2, comparisons: 3
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps

Knowledge Booster
Similar questions
- Write up Java source codes for compiling and execution of program below. Draw UML diagram for codes. There is a very simple solution to keeping Tic-tac-toe fresh and interesting, though, and it has been thought up by a group of mathematicians. It’s being called Ultimate Tic-tac-toe and simply embeds a Tic-tac-toe board in each of the nine squares of the original game. Now instead of just winning the main board, you also need to win each of the smaller boards first until you have a line of three. New Rule: You can only place a mark on the board determined by the position of your opponent’s last placed mark. So, if they put an X or O in the top right corner of a square, your next move must occur in the top right board. By adding that rule the game is no longer about winning a single board, it’s about tactically managing up to 9 games at once and plotting ahead. You still win by marking three squares in a row, but that now involves winning three games. The effects of the New Rule: 1:…arrow_forwardIn Java, hiding implementation details is done by marking data and methods as : a private b protected c public d defaultarrow_forwardWhat do you mean when you say "angular expressions"? What distinguishes Angular expressions from JavaScript expressions?arrow_forward
- Make a java airline reservation system. Things the program should have: - Main menu(options to cancel, book and reschedule flights) - Use things like constructors, array lists, etc. - Print out your final ticket with the date of the flight, number and airline.arrow_forwardCreate a java for loop that maps denotational semantics Please walk me through this step by step!!!arrow_forward4. Identify the benefits of using both Accessor and Mutator in Java. PS: Please answer it correctly and less confusing.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education