With the following code answer the following questions.
describe what happens when the following code is executed:
/**
* 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));
}
}Q.
Ans : - An object is being created here to store strings(words) in an array.
Q.System.out.println(linearFind("cat",searchMe));
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));
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.
With the following code answer the following questions. describe what happens when the following code is...
HI there I tried to run this code as an example given by my teacher, but when I copied and tried to run, then it ask to create BagInterface and ArrayBag class. Do i have to do something more previously in order to run this code. Can anyone help me her please? import java.util.Arrays; public class BagTraceExample { public static void main(String[] args) { BagInterface<String> animalBag = new ArrayBag(); System.out.println(animalBag.isEmpty());...
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,...
To do: Now add code to Pet and PetPlay to complete the comments in the code. import java.util.ArrayList; public class PetPlay { //----------------------------------------------------------------- // Stores and modifies a list of pets //----------------------------------------------------------------- public static void main (String[] args) { // always make your array lists generic ArrayList pets = new ArrayList(); pets.add(new Pet("dog", "Bella")); pets.add(new Pet("cat","Blackie")); pets.add(new Pet("bird", "Babs")); pets.add(new Pet("dog", "Lassie")); pets.add(new Pet("horse", "Sam")); pets.add(new Pet("dog", "Blackie")); pets.add(new Pet("turtle", "Joe")); pets.add(new Pet("bird","Annie")); pets.add(new Pet("bird", "Alex"));...
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 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 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 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 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...
Need // descriptions for each line of code that is relevant (Example, a description of "arr[i]=r.nextInt(100)+1;".Will give positive rate import java.util.Random; public class TestApp { public static void main(String[] args) { int arr[]=new int[11]; Random r = new Random(); //Random function for generation of random numbers for(int i=0;i<arr.length;i++) // arr[i]=r.nextInt(100)+1; printArray(arr); System.out.println(); bubbleSort(arr); printArray(arr); System.out.println(); System.out.println("Median : "+(arr[arr.length/2])); } private static void printArray(int[] aArray) { for (int i...