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 }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter12: Points, Classes, Virtual Functions And Abstract Classes
Section: Chapter Questions
Problem 29SA
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
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage