Search for the character Z using the binary search algorithm on the following array of characters: A DHJLNPRZ

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter6: Arrays
Section: Chapter Questions
Problem 19RQ
icon
Related questions
Question

java

Search for the character Z using the binary search algorithm on the
following array of characters: ADHJLNPRZ
For each iteration of binary search use a table similar to the table
below to list: (a) the left index and (b) the right index of the array
that denote the region of the array that is still being searched, (c)
the middle point of the region of the array that is still being searched, and (d) the character-to-
character number of comparisons made during the search (line 09 and line 11 of the Binary
Search algorithm).
01 public static int binarySearch (char [] a, char target) {
02
03
int left = 0;
04
int right = a.length - 1;
05
06
while ( left <= right {
07
int middle = ( left + right ) / 2;
if ( a[middle]
08
09
== target ) {
10
return middle;
} else if ( target < a[middle] ) {
11
right = middle - 1;
} else {
12
13
14
left = middle + 1;
15
}
16
}
return -1; // target not found
18 }
17
Iteration
Left | Right | Middle | Number of Comparisons
1
3
Enter your answer here
Transcribed Image Text:Search for the character Z using the binary search algorithm on the following array of characters: ADHJLNPRZ For each iteration of binary search use a table similar to the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point of the region of the array that is still being searched, and (d) the character-to- character number of comparisons made during the search (line 09 and line 11 of the Binary Search algorithm). 01 public static int binarySearch (char [] a, char target) { 02 03 int left = 0; 04 int right = a.length - 1; 05 06 while ( left <= right { 07 int middle = ( left + right ) / 2; if ( a[middle] 08 09 == target ) { 10 return middle; } else if ( target < a[middle] ) { 11 right = middle - 1; } else { 12 13 14 left = middle + 1; 15 } 16 } return -1; // target not found 18 } 17 Iteration Left | Right | Middle | Number of Comparisons 1 3 Enter your answer here
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Introduction to computer system
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
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
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
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning