IN JAVA Write a method that implements the following binary

IN JAVA-

Write a method that implements the following binary search algorithm pseudocode:

Thanks! :D

Program 9.7 (not a complete program) 1 The binarysearch function accepts as arguments an Integer 2 array, a value to search the array for and the size 3 of the array. If the value is found in the array, its 4 subscript is returned Otherwise, -1 is returned, 5 indicating that the value was not found in the array 6 Function Integer binarysearch (Integer array[], Integer value, Integer arraysize) Variable to hold the subscript of the first element. Declare Integer first 0 10 Variable to hold the subscript of the last element. Declare Integer last arraysize 1 12 13 Position of the search value 14 Declare Integer position 15 16 Flag 17 Declare Boolean found False 18 19 Variable to hold the subscript of the midpoint 20 Declare Integer middle 21 22 23 While (NOT found) AND (first last) Calculate the midpoint 24 Set middle first last) 2 25 26 See if the value is found at the midpoint. 27 28 If array middle value Then Set found True 29 Set position middle 30 31 Else, if the value is in the lower half 32 33 Else I f array[middle j value Then 34 Set last middle 35 Else, if the value is in the upper half 36 37 Else Set first middle 1 38 End If 39 End While 40 41 Return the position of the item, or -1 42 if the item was not found. 4 3 44 Return position 45 End Function

Solution

public category MyBinarySearch begin = 0;
int finish = inputArr.length - 1;
whereas (start <= end) middle = (start + end) / 2;
if (key == inputArr[mid]) come back mid;
}
if (key < inputArr[mid]) {
   finish = middle - 1;
} else begin = middle + 1;
}
}
return -1;
}

public static void main(String[] args) ;
   System.out.println(\"Key 14\'s position: \"+mbs.binarySearch(arr, 14));
   int[] arr1 = ;
   System.out.println(\"Key 432\'s position: \"+mbs.binarySearch(arr1, 432));
}
}

IN JAVA- Write a method that implements the following binary search algorithm pseudocode: Thanks! :D Program 9.7 (not a complete program) 1 The binarysearch fun

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site