Question: Write a Java program to play the game of searching a key from a sorted array. The sorted integer array must be defined exactly as

Write a Java program to play the game of searching a key from a sorted array. The sorted integer array must be defined exactly as follows: private static int [] A = {//80 integer numbers in this array. Value range: 1 to 102. 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, 44, 45, 46, 47, 48, 49, 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, 92, 93, 95, 96, 97, 98, 99, 100, 101, 102 }//skip numbers 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 91, 94 You must also declare the following 2 variables to count comparisons for both searching methods: private static int countBinS = 0;//to count # of comparisons used by binarySearch () method private static int countSeqS = 0;//to count # of comparisons used by sequentialSearch () method You must create the following 2 methods: (a)public static int binarySearch(int [] A, int key)//can use the Java code on page 530 (b)public static int sequentialSearch(int [] A, int key)//get idea from page 528: indexOfWinner You need to add countBinS++: statement to each if-comparison block in binarySearch () method. You also need to add countSeqS++: statement to each if-comparison block in sequentialSearch () method. You should reset both counters to zero before each game is started. The following is a sample test, which must be your test case #1. In addition, you must also do test case #2 and test case #3 with different set of 7 search key values. Please note that your test case #1 may not show the exact numbers of comparisons as shown below, but they should be very close. Welcome to this game of searching a key using both binary and sequential searches! Please enter the number to search (-1 to stop the game) > 0 0 is not found. Binary search used 6 comparisons, Sequential search used 0 comparisons. Please enter the number to search (-1 to stop the game) > 103 103 is not found. Binary search used 7 comparisons, Sequential search used 80 comparisons. Please enter the number to search (-1 to stop the game) > 33 33 is found. Binary search used 6 comparisons, Sequential search used 33 comparisons. Please enter the number to search (-1 to stop the game) > 94 94 is not found. Binary search used 6 comparisons, Sequential search used 72 comparisons. Please enter the number to search (-1 to stop the game) > 80 80 is found. Binary search used 2 comparisons, Sequential search used 60 comparisons. Please enter the number to search (-1 to stop the game) > 20 20 is found. Binary search used 2 comparisons, Sequential search used 20 comparisons. Please enter the number to search (-1 to stop the game) > 102 102 is found. Binary search used 7 comparisons. Sequential search used 80 comparisons. Please enter the number to search (-1 to stop the game) > -1 Thank you for playing this game of key searching! Hope to see you again
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
