



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
10.2.8 Maximum Iterations CodeHS Answers
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!
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; } }