Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

10.2.8 Maximum Iterations CodeHS Answers, Assignments of Marketing Business-to-business (B2B)

10.2.8 Maximum Iterations CodeHS Answers

Typology: Assignments

2024/2025

Available from 05/25/2025

CrackSmart
CrackSmart 🇺🇸

109 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
10.2.8 Maximum Iterations CodeHS
Answers
The most common answer to the 10.2.8 Maximum Iterations Java Codehs
answer is:
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.
Scanner input = new Scanner(System.in);
System.out.println("Array Size: 100");
System.out.println("Max iterations: " + binaryMax(100));
binaryRec(generateArrayOfLength(100), 2, 0, 99);
System.out.println("Actual iterations: " + count);
System.out.println();
count = 0;
System.out.println("Array Size: 1000");
pf3
pf4
pf5

Partial preview of the text

Download 10.2.8 Maximum Iterations CodeHS Answers and more Assignments Marketing Business-to-business (B2B) in PDF only on Docsity!

10.2.8 Maximum Iterations CodeHS

Answers

The most common answer to the 10.2.8 Maximum Iterations Java Codehs

answer is:

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. Scanner input = new Scanner(System.in); System.out.println("Array Size: 100"); System.out.println("Max iterations: " + binaryMax(100)); binaryRec(generateArrayOfLength(100), 2, 0, 99); System.out.println("Actual iterations: " + count); System.out.println(); count = 0; System.out.println("Array Size: 1000");

System.out.println("Max iterations: " + binaryMax(1000)); binaryRec(generateArrayOfLength(1000), 2, 0, 999); System.out.println("Actual iterations: " + count); System.out.println(); count = 0; System.out.println("Array Size: 10000"); System.out.println("Max iterations: " + binaryMax(10000)); binaryRec(generateArrayOfLength(10000), 2, 0, 9999); System.out.println("Actual iterations: " + count); System.out.println(); count = 0; System.out.println("Array Size: 100000"); System.out.println("Max iterations: " + binaryMax(100000)); binaryRec(generateArrayOfLength(100000), 2, 0, 99999); System.out.println("Actual iterations: " + count); } public static int binaryRec(int[] array, int target, int begin, int end) { if (begin <= end) {

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; } }

This Java code implements a Binary Search Test, which includes methods for

performing a binary search recursively (binaryRec), generating a sorted array of a

given length (generateArrayOfLength), and calculating the maximum number of

iterations required to perform a binary search on an array of a given size

(binaryMax).

The program runs tests for arrays of different sizes (100, 1000, 10000, 100000) and

prints the maximum iterations and actual iterations required to find a target value

using binary search.

Let’s walk through the key parts of the code:

1. binaryRec Method : Performs recursive binary search. It increments a global

counter count to track the number of iterations (or recursive calls) made during the

search process.

2. generateArrayOfLength Method : Generates a sorted array of random integers of

a specified length. It fills the array with random numbers and then sorts it.

3. binaryMax Method : Calculates the maximum number of iterations needed to

perform a binary search on an array of a specified size. This is based on the principle

that the maximum number of steps in a binary search is log2(n) + 1, where n is the

array size.

4. Main Method : Runs the binary search test for arrays of sizes 100, 1000, 10000, and

100000. It first prints the array size and the calculated maximum iterations. Then it

performs a recursive binary search on a target value (in this case, always 2 ) within a

newly generated array and prints the required number of iterations.

The target value 2 is used consistently for simplicity, but since the arrays are

randomly generated, the actual presence and position of 2 in each array can vary,

affecting the actual iterations count.

It’s worth noting that the arrays are filled with random numbers and sorted, and the

target for the binary search is set to 2. Given the randomness, the “Actual

iterations” might vary or the target might not be present at all in some runs, which

is a suitable demonstration of binary search behavior in various scenarios.

This Java code is well-structured for educational purposes. It illustrates how binary

search works, how to implement it recursively, and how to assess its efficiency in

terms of iterations.