Question

#2Trace These JAVA searches by hand on the following dataset. Sorted Data Set: 3, 5, 8,...

#2Trace These JAVA searches by hand on the following dataset.

Sorted Data Set: 3, 5, 8, 9, 12, 14, 18, 19, 21, 23, 24, 26

#Trace the sorted data set using a binary search.

Search target: 23

#Trace the sorted data set using a binary search.

Search target: 15

(To trace a binary search, list the value of first, last, and mid for each pass through the method.)

Use this method:

private static <T extends Comparable<? super T>>
   boolean binarySearch(T[] anArray, int first, int last, T desiredItem)
{
   boolean found;
   int mid=first+(last-first)/2;
  
   if (first>last)
       found = false;
   else if (desiredItem.equals(anArray[mid]))
       found true;
   else if (desiredItem.compareTo(anArray[mid])<0)
       found = binarySearch(anArray, first, mid-1, desiredItem);
   else
       found = binarySearch(anArray, mid +1, last, desiredItem);
   return
       found;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Sorted data set given as follows:

3          5          8          9          12        14        18        19        21        23        24        26

  1. To trace the data set using binary search method given with search target= 23 the passes and iterations are as follows:

Iteration

1

2

3

4

First

1

7

10

10

Last

12

12

12

11

Mid

6

9

11

10

Array[mid]

14

21

24

23

desiredItem.compareto(Array[mid])

23>14

23>21

23<24

23=23

index

6

9

11

10

So, the number 23 is found on the index number 10 and in 4th iteration

.

  1. To trace the data set using binary search method given with search target =15 the passes and iterations are as follows:

Iteration

1

2

3

4

5

6

7

8

First

1

7

7

7

7

7

7

7

Last

12

12

11

10

9

8

7

6

Mid

6

9

9

8

8

7

7

6

Array[mid]

14

21

21

19

19

18

18

14

desiredItem.compareto(Array[mid])

15>14

15<21

15<21

15<19

15<19

15<18

15<18

15>14

Index

6

9

9

8

8

7

7

6

So, we found that the number 15 is not present in a given sorted list. But, if we want to place the number 15, then it can be placed in between index number 6 and 7.

Add a comment
Know the answer?
Add Answer to:
#2Trace These JAVA searches by hand on the following dataset. Sorted Data Set: 3, 5, 8,...
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
  • Java The following questions ask about tracing a binary search. To trace the binary search, list...

    Java The following questions ask about tracing a binary search. To trace the binary search, list the value of first, last, and mid for each pass through the loop or method. Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target)...

  • Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) {...

    Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target) { targetLocation = mid; found = true; } else if (numbers[mid] < target) { first = mid + 1; } else { // numbers[mid] > target last = mid - 1;...

  • Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application...

    Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...

  • Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

    Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...

  • Need help with this Java project implementing an interpolation search, bolded the missing parts: /**   ...

    Need help with this Java project implementing an interpolation search, bolded the missing parts: /**    A class that implements an interpolation search and    a binary search of a sorted array of integers.    @author Frank M. Carrano    @author Timothy M. Henry    @version 4.0 */ public class SearchComparer {    private int[] a;    private int interpolationSearchCounter;    private int binarySearchCounter;    private static final int CAPACITY = 50;    public SearchComparer(int[] array, int n)    {...

  • //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded....

    //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...

  • Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following...

    Question from Object-Oriented Data Structures Using Java 4th Edition Chapter 5 Question 30 Add the following methods to the LinkedCollection class, and create a test driver for each to show that they work correctly. Code each of these methods by accessing the internal variables of the LinkedCollection, not by calling the previously defined methods of the class.String toString() creates and returns a string that correctly represents the current collection. 1. Such a method could prove useful for testing and debugging...

  • 6.) (a) A string contains several X's followed by several O's. Devise a divide-and-conquer algorithim that finds the number of X's in the string in log2n steps, where n is the length of th...

    6.) (a) A string contains several X's followed by several O's. Devise a divide-and-conquer algorithim that finds the number of X's in the string in log2n steps, where n is the length of the string. (b) An array originally contained different numbers in ascending order but may have been subsequently rotated by a few positions. For example, the resulting array may be: 21 34 55 1 2 3 4 5 8 13 Is it possible to adapt the Binary Search...

  • Can someone help me with the main class of my code. Here is the assignment notes....

    Can someone help me with the main class of my code. Here is the assignment notes. Implement project assignment �1� at the end of chapter 18 on page 545 in the textbook. Use the definition shown below for the "jumpSearch" method of the SkipSearch class. Notice that the method is delcared static. Therefore, you do not need to create a new instance of the object before the method is called. Simply use "SkipSearch.jumpSearch(...)" with the appropriate 4 parameter values. When...

  • Question 3 Searching Algorithms [12 marks (4 marks) Define a method that when passed an array...

    Question 3 Searching Algorithms [12 marks (4 marks) Define a method that when passed an array of integers arr and another integer target, returns the last index in arr where target exists and -1 if target isn't found in arr a. b. (4 marks) Provide a trace, in the form of a logic table, of running the following method for 1, 4, 4, 4, 5, 7, 8, 12, 12, 15, 16, 17, 35, 35, 35, 40 arr = target 14...

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