How many times does a binary search need to execute to find its value? Recall from our lesson that the number of iterations is roughly a log base 2 relationship. In this exercise, you are going to calculate the maximum iterations and the actual iterations needed to find a random value in arrays of size 100, 1000, 10k, and 100k. You are given helper methods to calculate the maximum iterations, generate the random array, and do the binary search. You are also given a counter variable that increments up each time your recursive binary method is called. You will need to come up with the remainder of the code. Same output is provided below. Sample Output Array Size: 100 Max iterations: 7 Actual iterations: 5 Array Size: 1000 Max iterations: 10 Actual iterations: 5     import java.util.*; public class BinarySearchTest { static int count; public static void main(String[] args) { // Use the helper code to generate arrays, calculate the max // iterations, and then find the actual iterations for a randomly // selected value. } public static int binaryRec(int[] array, int target, int begin, int end) { if (begin <= end) { int mid = (begin + end) / 2; count ++; // Base Case if (target == array[mid]) { return mid; } if (target < array[mid]) { return binaryRec(array, target, begin, mid - 1); } if (target > array[mid]) { return binaryRec(array, target, mid + 1, end); } } return -1; //Alternate Base Case - not found } public static int[] generateArrayOfLength(int length) { int[] arr = new int[length]; for(int i = 0; i < length; i++) { arr[i] = (int)(Math.random() * 100); } Arrays.sort(arr); return arr; } private static int binaryMax(int length) { return (int) (Math.log(length) / Math.log(2)) + 1; } }

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

How many times does a binary search need to execute to find its value? Recall from our lesson that the number of iterations is roughly a log base 2 relationship.

In this exercise, you are going to calculate the maximum iterations and the actual iterations needed to find a random value in arrays of size 100, 1000, 10k, and 100k.

You are given helper methods to calculate the maximum iterations, generate the random array, and do the binary search. You are also given a counter variable that increments up each time your recursive binary method is called.

You will need to come up with the remainder of the code. Same output is provided below.

Sample Output

Array Size: 100

Max iterations: 7

Actual iterations: 5

Array Size: 1000

Max iterations: 10

Actual iterations: 5

 

 

import java.util.*;

public class BinarySearchTest {

static int count;

public static void main(String[] args) {

// Use the helper code to generate arrays, calculate the max
// iterations, and then find the actual iterations for a randomly
// selected value.

}

public static int binaryRec(int[] array, int target, int begin, int end) {
if (begin <= end)
{
int mid = (begin + end) / 2;
count ++;
// Base Case
if (target == array[mid]) {
return mid;
}

if (target < array[mid]) {
return binaryRec(array, target, begin, mid - 1);
}

if (target > array[mid]) {
return binaryRec(array, target, mid + 1, end);
}
}
return -1; //Alternate Base Case - not found
}

public static int[] generateArrayOfLength(int length)
{
int[] arr = new int[length];
for(int i = 0; i < length; i++)
{
arr[i] = (int)(Math.random() * 100);
}

Arrays.sort(arr);

return arr;
}

private static int binaryMax(int length) {
return (int) (Math.log(length) / Math.log(2)) + 1;
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps

Blurred answer
Knowledge Booster
Arrays
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
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