Question

With the following code answer the following questions. describe what happens when the following code is...

With the following code answer the following questions. 

describe what happens when the following code is executed:

  1. String[] searchMe = {"apple","bear","cat","dog","elephant"};
    1. describe what is being created when this statement executes
  2. System.out.println(linearFind("cat",searchMe));
    1. describe the values passed to the method
    2. describe how each of the specific values are compared to each other
    3. describe when the method stops executing and/or when the loop stops executing
    4. describe what is returned to beoutprinted
  3. System.out.println(binaryFind("apple",searchMe));
    1. describe the values passed to the method
    2. describe how each of the specific values are compared to each other
    3. describe when the method stops executing and/or when the loop stops executing
    4. describe what is returned to be outprinted
  • Describe how the selection sort algorithm, the bubble sort algorithm, and the insertion sort algorithm are different. In other words, briefly explain how they sort data.
  • /**
     * Class that demonstrates search algorithms
     * @author Mark Guzdial
     * @author Barb Ericson
     **/
    public class Searcher
    {
    
      /**
       * Implement a linear search through the list
       **/
      public static String linearFind(String target, String[] list)
      {
        for (int index=0; index < list.length; index++)
        {
          if (target.compareTo(list[index]) == 0)
          {return("Found it!"); }
        }
        return("Not found");
      }
      
       /**
       * Method to use a binary search to find a target string in a
       * sorted array of strings
       */
      public static String binaryFind(String target, String[] list)
      {
        int start = 0;
        int end = list.length - 1;
        int checkpoint = 0;
    
        while (start <= end)
        { //While there are more to search
          // find the middle
          checkpoint = (int)((start+end)/2.0);
          System.out.println("Checking at: "+
           checkpoint+" start="+start+" end="+end);
          if (target.compareTo(list[checkpoint]) == 0)
          {
            return "Found it!";
          }
          else if (target.compareTo(list[checkpoint]) > 0)
          {
            start=checkpoint + 1;
          }
          else if (target.compareTo(list[checkpoint]) < 0)
          {
            end=checkpoint - 1;
          }
        }
        return "Not found";
      }
      
      /** main for testing linearFind */
      /*
      public static void main(String[] args)
      {
        String[] searchMe = {"apple","bear","cat","dog","elephant"};
        System.out.println(linearFind("apple",searchMe));
        System.out.println(linearFind("cat",searchMe));
        System.out.println(linearFind("giraffe",searchMe));
      }
      */
    
    
      /**
       * Main for testing binary find 
       */
      public static void main(String[] args)
      {
        String[] searchMe = {"apple","bear","cat","dog","elephant"};
        System.out.println(binaryFind("apple",searchMe));
        System.out.println(binaryFind("cat",searchMe));
        System.out.println(binaryFind("giraffe",searchMe));
      }
    }
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q.

  1. String[] searchMe = {"apple","bear","cat","dog","elephant"};
    1. describe what is being created when this statement executes

Ans : - An object is being created here to store strings(words) in an array.

Q.System.out.println(linearFind("cat",searchMe));

  1. describe the values passed to the method

Ans:- a string is passed which is "cat" along with the String array(object) which is String[] searchMe = {"apple","bear","cat","dog","elephant"}; as searchMe.

Q.describe how each of the specific values are compared to each other

Ans:- each of the specific values are compared to each other using compareTo method predefined in java. In this method, strings are converted to n the basis of the Unicode value of each character in the strings. It returns a value in integer format either +ve or -ve or 0.

Q.describe when the method stops executing and/or when the loop stops executing

Ans:- the method, as well as loop, stops either when we get two string (target is found in the array) equal based on Unicode or when we reach the end of the array and we haven't found the target string in the array. Loop stop executing when all the string in the array is traversed.  

Q. describe what is returned to beoutprinted

Ans:- If the string is found in the array "Found it!" is printed out and when the string is not found in the list then "Not found" is printed.

Q. System.out.println(binaryFind("apple",searchMe));

  1. describe the values passed to the method

Ans:- (same as in linearFInd) a string is passed which is "cat" along with the String array(object) which is String[] searchMe = {"apple","bear","cat","dog","elephant"}; as searchMe.

Q.describe how each of the specific values are compared to each other

Ans:- When we search for apple start = 0, end = 4, so checkpoint = 2 apple is less than cat so end changes to checkpoint – 1 start = 0, end = 1, so checkpoint = 0 We find apple at index 0 and return

Q. describe when the method stops executing and/or when the loop stops executing

Ans:- If we find the string at the midpoint method as well as loop stop executing. And even if start is equal to or greater then end loop stop executing.

Q. describe what is returned to be outprinted

Ans:-

Checking at: 2 start=0 end=4 Checking at: 0 start=0 end=1 Found it! Checking at: 2 start=0 end=4 Found it! Checking at: 2 start=0 end=4 Checking at: 3 start=3 end=4 Checking at: 4 start=4 end=4 Not found

Q.Describe how the selection sort algorithm, the bubble sort algorithm, and the insertion sort algorithm are different. In other words, briefly explain how they sort data.

ans:- Bubble sort repeatedly compares and swaps adjacent elements in every iteration. This algorithm is not fitting for large data sets as its standard and worst-case complexity are of Ο(n2) where n is the number of items.

In Selection sort algorithm we find the minimum value from the unsorted array and swap it with the first element. then we start finding again the minimum value but we do not include the first element as it is already sorted.

Insertion sort is based on the concept that one element from the input element is used in each pass to find its correct place i.e, the place to which it belongs in a sorted array. It compares the current element with the largest value in the sorted array. If the current element is greater, then it transmits the element in its place and moves on to the next element else it finds its correct position in the sorted array and passes it to that position. This is done by shifting all the elements, which are larger than the current element, in the sorted array to one position ahead.

Add a comment
Know the answer?
Add Answer to:
With the following code answer the following questions. describe what happens when the following code is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int...

    must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...

  • Please answer both questions. Thanks Question 24 (1 point) What value would be returned if the...

    Please answer both questions. Thanks Question 24 (1 point) What value would be returned if the method mystery were called and passed the array values (1, 15, 37, 12) as its parameter? public static int mystery (int[] list) int x = 0; for (int i = 1; i < list.length; i++) { int y = list[i] - list [0]; if (y > x) x = Y } } return x; 29 35 1 36 Question 25 (1 point) What is...

  • Consider java for fixing this code please: what i need is to insert method to be...

    Consider java for fixing this code please: what i need is to insert method to be added ( please don't change the test class and any giving value in the first class ) here is the correct out put: ------------------testAddLast()---- {A} {A->B} {A->B->null} {A->B->null->C} ----------------------------- --------testSubListOfSmallerValues()---------- {} {B->B->B->A} {F->B->B->B->A->D} {F->B->B->G->B->A->M->D} ----------------------------- ------------Test lastIndexOf()----- -1 3 -1 -1 0 5 2 ----------------------------- ---------testRetainAll()--------- {} {6:Tony->6:Tony} {null->bad->null} ----------------------------- ---------------Test removeStartingAtBack--- false true {apple->null->bad->null} true {apple->null->bad} {2:Morning->3:Abby->4:Tim->5:Tom->6:Tony} ----------------------------- ---------test insertionSort()--------- {} {D} {D->E->E->F->G}...

  • 6. (4 points) The following code for insertion sort has been modified to print out the...

    6. (4 points) The following code for insertion sort has been modified to print out the sorted component of the array during the sort. What will be the output of running the main method of the following code? Try to trace by hand and then verify by executing the code public class Insertion ( public static void sort (String[] a) f int n- a.length; for (int i-1; i < n; i++) f for (int j i; j 0; j--) if...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements...

    The file Sorting.java contains the Sorting class from Listing 9.9 in the text. This class implements both the selection sort and the insertion sort algorithms for sorting any array of Comparable objects in ascending order. In this exercise, you will use the Sorting class to sort several different types of objects. 1. The file Numbers.java reads in an array of integers, invokes the selection sort algorithm to sort them, and then prints the sorted array. Save Sorting.java and Numbers.java to...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT