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 }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
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
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
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
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education