Question: Question after this code: import javax.swing.JOptionPane; /** * Class that demonstrates search algorithms * **/ public class Searcher { /** * Implement a linear search

Question after this code:

import javax.swing.JOptionPane;

/** * Class that demonstrates search algorithms * **/ 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"}; // note the use of an input dialog box. What type of data does it return to us? String term = JOptionPane.showInputDialog("what do you want to search for?"); // note the use of a message dialog box JOptionPane.showMessageDialog(null,term + " - " + linearFind(term,searchMe)); // can you replace the hard coded search values in the // next 2 statements using input dialog boxes? JOptionPane.showMessageDialog(null,linearFind("cat",searchMe)); JOptionPane.showMessageDialog(null,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)); } */ }

////////////

In order to better understand how the linear and binary search algorithms perform their tasks, we are going to walk through (describe step by step) an example of each.

Using the Searcher.java logic posted, describe what happens when the following code is executed:

1.String[] searchMe = {"apple","bear","cat","dog","elephant"};

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

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

In addition to describing the results of each of these statements, make sure you describe the details of what happens in each of the methods that are called: when is the loop condition checked, what are the values of the local variables and when do they change, at what point do we return from each method, and what data gets returned?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!