Question: Java Based Programming. In the following code show the number of steps in the linear search and binary search process. The number of basic steps
Java Based Programming. In the following code show the number of steps in the linear search and binary search process.
The number of basic steps needs to be calculated in both linearSearch and binarySearch methods.
Fields have been provided to store count.
Notes:
- Basic step in linear search is comparison.
- Basic step in binary search is finding the middle element of the array.
- To find the number of basic steps, you should find the number of loop iterations in each search method.
- An incomplete version of the code for this lab exercise is provided. You should make required changes in both search methods and complete the main method.
//CODE THAT NEEDS TO BE EDITED
import java.security.SecureRandom; import java.util.Arrays; import java.util.Scanner;
public class Search { static int linearSteps = 0; static int binarySteps = 0;
// perform a linear search on the data public static int linearSearch(int data[], int searchKey) { // loop through array sequentially for (int index = 0; index
// perform a binary search on the data public static int binarySearch(int[] data, int key) { int low = 0; // low end of the search area int high = data.length - 1; // high end of the search area int middle = (low + high + 1) / 2; // middle element int location = -1; // return value; -1 if not found do // loop to search for element { // if the element is found at the middle if (key == data[middle]) location = middle; // location is the current middle else if (key
} // end main } // end class Search

Array [35, 26, 29, 48, 61, 67, 57, 64, 40, 73, 62, 31, 56, 23, 37, 39, 27, 66, 44, 64, 66, 33, 20, 30, 52, 38, 44, 33, 53, 48 Please enter an integer value (-1 to quit) 52 LINEAR SEARCH 52 was found in position 24 Number of basic steps: 25 Sorted Array [20, 23, 26, 27, 29, 30, 31, 33, 33, 35, 37, 38, 39, 40, 44, 44, 48, 48, 52, 53, 56, 57, 61, 62, 64, 64, 66, 66, 67, 73] BINARY SEARCH-- 52 was found in position 18 Number of basic steps:5 Please enter an integer value (-1 to quit) 63 -LINEAR SEARCH- 63 was not found Number of basic steps: 30 Sorted Array 20, 23, 26, 27, 29, 30, 31, 33, 33, 35, 37, 38, 39, 40, 44, 44, 48, 48, 52, 53, 56, 57, 61, 62, 64,64, 66, 66, 67, 73] BINARY SEARCH- 63 was not found Number of basic steps: 5 Please enter an integer value to quit): 1
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
