Here is the code implementing binary search.
public class BinarySearch{ private static int arrayCheckSortedOrNot(int [] array){ int flag = 1; for (int i = 0; i < array.length - 1; i++) { if (array[i] > array[i + 1]) { flag = 0; break; } } return flag; } static int binarySearch(int [] values, int n, int target) { int R = n; int L = 1; int midIndx; // Index of middle element int stepCount = 0; // Steps taken if ( L > R){ System.out.println("The search terminates as unsuccessful"); System.exit(0); } while(R >= L) { midIndx = (L + R) / 2; stepCount++; System.out.println("The step required to search : " + stepCount); if (values[midIndx] < target){ L = (midIndx + 1); }else if (values[midIndx] > target){ R = (midIndx - 1); }else{ return midIndx; } } return -1; // When search element not found in the array } public static boolean resultFinal(int [] values, int n, int target){ // This method for returning int value int result = binarySearch(values, n, target); if(result == -1) { return false; }else { return true; } } public static void main(String [] args){ // Here is where you start the main call int [] values = {2, 5, 6, 7, 9, 10, 11, 12}; int n = values.length; int target = 7; int checkForUnsortedArray = arrayCheckSortedOrNot(values); if (checkForUnsortedArray==0){ System.out.println(checkForUnsortedArray); // Display 0 as the array is unsorted }else { resultFinal(values, n, target); //else enter binay search method } } }
(a) How many steps does it take for the input of v=[2, 5, 6, 7, 9, 10, 11, 12] and x = 7? List each step in order.
O/P :
The step required to search : 1
The step required to search : 2
The step required to search : 3
(b) Give an example of an unsorted vector v and an element x in v for which the algorithm would return an answer of 0.
Yes the code does return 0 for an unsorted array - Tested with {92, 5, 600, 7, 9, 10, 11, 12} - returned 0
(c) What’s the most number of steps it could take for a length 8 input?
Max number of steps would be 3 as the array will be divided into 2 halves.
(d) Estimate the most number of steps it could take for a length 1000 input? Explain.
It would approximately take log2(1000). More number of steps are taken when you are trying to search for an item which is not present in the array, or trying to find the left extreme or the right extreme items.
3. Write the function find sorted elt whose input is a vector sorted in increasing order...
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....
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"...
2. In class, we discussed the problem of searching for an item x in a sorted array L with n entries. The returned value is supposed to be -1 if the value x is not in the array, and the index i such that L[i] = x if x is in the array. Assume that I does not contain duplicate values. Now consider the decision trees of algorithms for solving this problem. To make the decision tree more explicit, we...
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...
Python 3, nbgrader, pandas 0.23.4 Q2 Default Value Functions (1 point) a) Sort Keys Write a function called sort_keys, which will return a sorted version of the keys from an input dictionary Input(s): dictionary :dictionary reverse boolean, default: False Output(s): .sorted_keys: list Procedure(s) Get the keys from the input dictionary using the keys()method (this will return a list of keys) Use the sorted function to sort the list of keys. Pass in reverse to sorted to set whether to reverse...
Given a matrix of size NxM sorted in descending order and value V, write a function which returns -1 if value V is not in the matrix, else it returns 1 if V is in the matrix. The function also computes the position of V in the matrix, that is P= [R, C], where R is the row number of V and C is the column number of V in the matrix, if V is in the matrix (if V...
Search for 4 in the sequence {3,5,7,8,9,12,21,25}, by working through each step of the algorithm given below. Specify the values of i, j, m and am in each step. procedure binary search (x: integer, a1,a2,...,an: increasing integers) i := 1 {i is left endpoint of search interval } j := n {j is right endpoint of search interval } while i < j m := b(i + j)/2c if x > am then i := m + 1 else j...
Meet is an operation between zero-one matrices and uses Boolean products (C. Join is an operation between zero-one matrices and uses Boolean products d. Join is an operation between zero-one matrices and uses conjunctions 29. Given set S (-2, -1, 0, 1, 2), R is a relation on S. i. R ( (x, y)| x-y 1} Write R as a set of ordered pairs. (Use roster method to write R as a set of tuples) 3pts ill. and matrix representation...
in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...
Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func) (cond ((null? input-list) '()) ((func (car input-list)) (cons (car input-list) (filter (cdr input-list) func))) (else (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...