(a) Decision Tree for Algorithm 1 is shown below:

(b) The Algorithm 1 is incorrect, because when the parameters first and last becomes equal, the algorithm directly returns -1 instead of comparing L[first] with x.
(c) Decision Tree for Algorithm 2 is shown below:

(d) The Algorithm 2 is also incorrect, because it runs into an infinite loop when the parameters first and last are equal.
// If you have any query do ask in the comments section.
2. In class, we discussed the problem of searching for an item x in a sorted...
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...
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...
When binary searching a sorted array that contains more than one
key equal to the search key, the client may want to know the index
of either the first or the last such key. Accordingly, implement
the following API: public class BinarySearchDeluxe { /* Returns the
index of the first key in a[] that equals the search key, or -1 if
no such key. */ public static int firstIndexOf(Key[] a, Key key,
Comparator comparator) /* Returns the index of the...
//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...
2. The following (adapted from Wikipedia) gives pseudocode for searching a sorted vector A for a specific element T. Given an array A of n elements with values or records A1 An and target value T, the following subroutine uses binary search to find the index of T in A. 1. Set L to 1 and R ton. 2. If L>R, the search terminates as unsuccessful Set m (the position of the middle element) to the floor of (L+R)/2. 3....
Consider an array 'a[n]' in which you are searching for the first occurrence of integer 'x'. With the given pseudocode: for (i=0; I < n; i++) if (x==a[i]) return i; if (i==n) return -2; e) x=5; a[] = {3,2,5,7,1}; How many times the loop is executed? f) x=5; a[] = {5,2,3,7,1]; How many times the loop is executed? g) x=5; a[] = {6,2,3,7,1}; How many times the loop is executed? h) Provide example of an input sequence which would...
3. Write the function find sorted elt whose input is a vector sorted in increasing order and an element x. The output is 0 if the element x does not occur in v, 1 if the element x does occur in v. Below is the algorithm you should implement, known as the binary search algorithm. Note that the code below returns the index, but we want to return true or false, not the index, so adjust your code accordingly. Algorithm....
Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...
Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...
A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...