Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i. Write a program that Calls a method that performs sequential search. The meth

ID: 3735659 • Letter: I

Question

i. Write a program that Calls a method that performs sequential search. The method accepts two parameters: an array of integers and an integer. It returns the index of the integer in the array if it is found else it returns b. Calls another method that takes two parameters, an integer and a sorted integer array. The call should perform binary search and should return the index of the integer in the array if found and should return -(insertionPoint+ 1) if the number is not found. Print out the index in the main method. e. Calls a third method that performs binary search recursively. It should take 4 parameters: 3 integers and a sorted array of integers. It should return the index of the element if found, if not then it should return -1

Explanation / Answer

a. Sequential search,biary search with or without recursive:

package chegg;

import java.util.Arrays;

public class SequentialSerach {

   public static void main(String[] args) {
       int[] arrValue = {2,5,20,-50,60,200};//create a array with predefined value
       int key=-50; //define a key value to search
        int found = sequentialSearch(arrValue,key); // declare a variable and call a method sequentialSearch pass the parameter to array and search key value
        if(found==-1) //if found is -1 then
          System.out.println(key +" not found "); //not found
        else //if found is 1 then
          System.out.println(key +" found "); //found key
        key=25;
        //second search value which is not in array
        found = sequentialSearch(arrValue,key); // declare a variable and call a method sequentialSearch pass the parameter to array and search key value
        if(found==-1) //if found is -1 then
          System.out.println(key +" not found "); //not found
        else //if found is 1 then
          System.out.println(key +" found "); //found key
      
        Arrays.sort(arrValue); // Sort using Java's built-in sort method!
      
      
      
        //BINARY SEARCH USING RECURSIVE METHOD
      
      
        key =60;
        System.out.println("Binary search with recursive ");
        found = binarySearch(arrValue, 0, arrValue.length-1, key); //binary search method
        if(found==-1) //if found is -1 then
            System.out.println(key +" not found "); //not found
       else //if found is 1 then
           System.out.println(key +" found "); //found key
      
        key =500;
        System.out.println("Binary search with recursive ");
        found = binarySearch(arrValue, 0, arrValue.length-1, key); //binary search method
        if(found==-1) //if found is -1 then
            System.out.println(key +" not found "); //not found
       else //if found is 1 then
           System.out.println(key +" found "); //found key
      
      
      
        //BINARY SEARCH WITHOUT RECURSIVE METHOD
      
      
        key =60;
        System.out.println("Binary Search without recursive ");
        found = binarySearch(arrValue,key); //binary search method
        if(found==-1) //if found is -1 then
            System.out.println(key +" not found"); //not found
       else //if found is 1 then
           System.out.println(key +" found"); //found key
      
        key =500;
        System.out.println("Binary Search without recursive");
        found = binarySearch(arrValue, key); //binary search method
        if(found==-1) //if found is -1 then
            System.out.println(key +" not found"); //not found
       else //if found is 1 then
           System.out.println(key +" found"); //found key
   }
  
  
   private static int binarySearch(int[] arrValue, int key) { ////binary search method using without recursion
       int low = 0; //declare low value with 0 initialize
        int high = arrValue.length - 1; //declare high value initalize the length of array
        while (high >= low) { //run while loop until high value is greater than low
            int middle = (low + high) / 2; //divide array length by 2
            if (arrValue[middle] == key) { //checks if the array of mid value is equal to key value
                return middle; //return mid value
            } else if (arrValue[middle] < key) { //checks if the array value is less than key value then
                low = middle + 1; //increment the mid value with 1 and put in low variable
            } else if (arrValue[middle] > key) { //checks if the array value is greater than key value then
                high = middle - 1; //decrement the mid value with 1 and put in low variable
            }
        }
        return -1;
   }
  
  
   private static int binarySearch(int[] arrValue, int l, int aL, int key) { //binary search method using recursion
       if (aL>=l)
        {   //divide the array value by 2
            int mid = l + (aL - l)/2;
            if (arrValue[mid] == key) //checks if the mid value is equal to key
               return mid; //return mid

            if (arrValue[mid] > key) //checks if mid value is smaller than key
               return binarySearch(arrValue, l, mid-1, key); //then call the binary search recursive method
            //otherwise
            else
               return binarySearch(arrValue, mid+1, aL, key); //call the binary search recursive method
        }
        return -1;
   }
  
   public static int sequentialSearch(int[] array, int keySearch){ //implement sequentialSearch method
       for (int j = 0; j < array.length; j++){ //loop to the length of array
             if (array[j] == keySearch){ //checks if array of index value is keySearch value then return j otherwise continue the untill the array legth
                return 1;
             }
        }
        return -1;
   }
}

output:

-50 found

25 not found

Binary search with recursive

60 found

Binary search with recursive

500 not found

Binary Search without recursive
60 found
Binary Search without recursive
500 not found