Question: Write the method int iterativeBinarySearch ( int [ ] arr, int value ) . This method should accept a sorted array of integers and return

"Write the method int iterativeBinarySearch(int[] arr, int value). This method should accept a sorted array of integers and return the index at which value occurs in the array, using an iterative implementation of the binary search algorithm. If the value does not occur in the array, your method should return -1.
Write the method int recursiveBinarySearch(int[] arr, int value). This method should accept a sorted array of integers and return the index at which value occurs in the array, using a recursive implementation of the binary search algorithm. If the value does not occur in the array, your method should return -1.
Both using this main method that cannot be modified.
/**
* A class for implementing binary search
* both iteratively and recursively
*
*/
public class BinarySearch {
public static void main(String[] args){
int[] nums ={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,
19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,
35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,
51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,
67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,
83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100};
long start =0, end =0, time =0;
int result =0;
//test1
start = System.nanoTime();
result = iterativeBinarySearch(nums,2);
end = System.nanoTime();
time =(end - start);
System.out.println(""Test 1: Iterative found ""+result+"" in ""+ time+""nanosec"");
//test2
start = System.nanoTime();
result = recursiveBinarySearch(nums,2);
end = System.nanoTime();
time =(end - start);
System.out.println(""Test 2: Recursive found ""+result+"" in ""+ time+""nanosec"");
//test3
start = System.nanoTime();
result = iterativeBinarySearch(nums,99);
end = System.nanoTime();
time =(end - start);
System.out.println(""Test 3: Iterative found ""+result+"" in ""+ time+""nanosec"");
//test4
start = System.nanoTime();
result = recursiveBinarySearch(nums,99);
end = System.nanoTime();
time =(end - start);
System.out.println(""Test 4: Recursive found ""+result+"" in ""+ time+""nanosec"");
//test5
start = System.nanoTime();
result = iterativeBinarySearch(nums,50);
end = System.nanoTime();
time =(end - start);
System.out.println( XF09""Test 5: Iterative found ""+result+"" in ""+ time+""nanosec"");
//test6
start = System.nanoTime();
result = recursiveBinarySearch(nums,50);
end = System.nanoTime();
time =(end - start);
System.out.println(""Test 6: Recursive found ""+result+"" in ""+ time+""nanosec"");
}
public static int iterativeBinarySearch(int[] arr, int value){
return -1;
}
public static int recursiveBinarySearch(int[] arr, int value){
return -1;
}
}"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!