public class BinarySearch2{ public static void main(String[] args) { int[] myList = {20, 12, 10, 7, 5, 3, 2); int result = binarySearch2 (myList, 20); if (result == -1) System.out.println("Not found!"); else System.out.println("The index of the input key is " + result+ " :
public class BinarySearch2{ public static void main(String[] args) { int[] myList = {20, 12, 10, 7, 5, 3, 2); int result = binarySearch2 (myList, 20); if (result == -1) System.out.println("Not found!"); else System.out.println("The index of the input key is " + result+ " :
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
Related questions
Question
Create a BinarySearch2.java to implement the binary search on a descending array. Create a
method for the binary search: “binarySearch2” in BinarySearch2.java. Implement your own code
in Eclipse. ensure it runs without errors.
Expert Solution
Step 1
Program Approach:-
1. Create the class BinarySearch2
2. Declare and initialize the array whose name is mylist
3. Call the function binarysearch2(mylist, 20)
4. Display the output
5. Create the user-defined function whose name is binarysearch2(int[] list, int key)
- Declare and initialize the variable
- int low1=0
- int high1=list.length-1
- Use the while loop to check the condition high1>=low1 when condition is true then go to step a otherwise go to step g
- a) mid1=(low1+high1)/2
- b) check the condition key<list[mid1] when condition is true then go to step c otherwise go to step d
- c) low1 = mid1+1
- d) check the condition key == list[mid1] when condition is true then go to step e otherwise go to step f
- e) return mid1
- f) high1=mid1-1
- g)return -1
Step by step
Solved in 4 steps with 1 images
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY