Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

Question

public int binarySearch(int[]array, int num) {
int low = 0;
//low range
int high = array.length -1; //high range
int mid; //mid range
while () //while low is less than or equal to high
{
mid = ; //set middle range to be (low + high) /2
if () { //if the array in the middle range = input number
//return mid range

}
else
if () { //if the array in the middle range > input number
//set the high value to be the mid value minus 1

}
else
{
//set low value to be mid value plus one

}
}
//return -1 here because that would mean that the number is not found in the loop
}

Complete the following binary search method. Find the number num in the array array.Return -1 if the number is not found. You may assume the array is properly ordered.
Examples:
binarySearch ({1,4,7},7) -> 2
binarySearch({2,6,6,8,80},6) -> 2
expand button
Transcribed Image Text:Complete the following binary search method. Find the number num in the array array.Return -1 if the number is not found. You may assume the array is properly ordered. Examples: binarySearch ({1,4,7},7) -> 2 binarySearch({2,6,6,8,80},6) -> 2
Your Answer:
1 public int binarySearch(int[ ]array, int num) {
int low = 0;
3
//low range
int high - array.length -1; //high range
int mid; //mid range
4
6.
while ()
//while low is less than or equal to high
7
{
8.
mid = ; //set middle range to be (low + high) /2
if () { //if the array in the middle range = input number
//return mid range
9.
10
11
12
13
else
14
if () { //if the array in the middle range > input number
15
//set the high value to be the mid value minus 1
16
17
}
18
else
19
{
20
//set low value to be mid value plus one
21
22
23
}
24
//return -1 here because that would mean that the number is not found in the loop
25 }
26
H H N N N N N N N
expand button
Transcribed Image Text:Your Answer: 1 public int binarySearch(int[ ]array, int num) { int low = 0; 3 //low range int high - array.length -1; //high range int mid; //mid range 4 6. while () //while low is less than or equal to high 7 { 8. mid = ; //set middle range to be (low + high) /2 if () { //if the array in the middle range = input number //return mid range 9. 10 11 12 13 else 14 if () { //if the array in the middle range > input number 15 //set the high value to be the mid value minus 1 16 17 } 18 else 19 { 20 //set low value to be mid value plus one 21 22 23 } 24 //return -1 here because that would mean that the number is not found in the loop 25 } 26 H H N N N N N N N
Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education