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 := m if x = ai then location := i else location := 0 return (location)
Fill your answers in the following worksheet. You may not use all of the blanks.
Steps |
i < j |
m |
am |
x > am |
i |
j |
0 |
T or F |
N/A |
N/A |
T or F |
1 |
8 |
1 |
||||||
2 |
||||||
3 |
||||||
4 |
||||||
5 |
||||||
6 |
||||||
7 |
||||||
8 |
||||||
Return |
location = |
Search for 4 in the sequence {3,5,7,8,9,12,21,25}, by working through each step of the algorithm given...
The following algorithm (Rosen pg. 363) is a recursive version of linear search, which has access to a global list of distinct integers a_1, a_2,..., a_n. procedure search(i, j, x : i,j, x integers, 1 < i < j < n) if a_i = x then return i else if i = j then 4. return 0 else return search(i + 1, j, x) Prove that this algorithm correctly solves the searching problem when called with parameters i = 1...
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...
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....
Fibonacci search algorithm Proof required (b) Prove that at each step of the Fibonacci search algorithm, p<q Hint: Prove the cases k-n (Step 2), k-n - 1,n - 2,...,3 (Step 3), and k -2 (Step 4) separately For the Step 3 case consider how the length of the interval is being reduced at each iteration. You will also need to use (and prove) 〉 -when Fk = , ,3 Alternative, correct proofs that do not use the hint are also...
ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence. [Hint: Use divide-&-conquer. Do the “combine” step carefully] B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of...
Given the following algorithm: Algorithnm Input: a1, a2,...,an, a sequence of numbers n, the length of the sequence x, a number Output: ?? i:- 1 While (x2 # a, and i < n) i+1 End-while If (x- - a) Return(i) Return(-1) 3, -1, 2,9, 36,-7, 6,4 a) What is the correct output of the Algorithm with the following input: a1, a2,..an b) What is the asymptotic worst-case time complexity of the Algorithm? Algorithnm Input: a1, a2,...,an, a sequence of numbers...
32. (6 points) Trace the Text Search Algorithm for the input t"0101001' and p "001 Input: p( indexed from 1 to m),m, t (indexed from 1 to n),n Output: i text search(p,m,t,n) f For i= 1 to n-m+1( while (ti+/-1-= pj){ jj+1 If (j> m) Return i return 0 32. (6 points) Trace the Text Search Algorithm for the input t"0101001' and p "001 Input: p( indexed from 1 to m),m, t (indexed from 1 to n),n Output: i text...
Consider an ordered array A of size n and the following ternary search algorithm for finding the index i such that A[i] = K. Divide the array into three parts. If A[n/3] > K. the first third of the array is searched recursively, else if A[2n/3] > K then the middle part of the array is searched recursively, else the last thud of the array is searched recursively. Provisions are also made in the algorithm to return n/3 if A[n/3]...
C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...
solve 6 please . step by step. #5. Given a sequence fa pao, a , a, , define the sequence lbne bobt b2, by 田 b.-ao + a1 +a2 + + ak If a (x) is the generating function for (an) and b(x) is the generating function for tbnj, then show (1-x)a. This lets us write b)4 #6. Let(anpao, ai, a2, given by a,-0, ai-1, an+2-an+1 + an be the sequence of Fibonacci numbers; recall that the ordinary generating function...