Question

In: Computer Science

// return index i such that A[i] = x, return -1 if x is not found...

// return index i such that A[i] = x, return -1 if x is not found

int mysearch(A, left, right, x) {

//add statements

}

(a) (10 pts) Given a sorted array A of size n, i.e. data has been sorted. Give a Divide and Conquer recursive “mysearch” algorithm which first tests the element at position n/4 for equality with some value x, and then possibly checks the element at position 3n/4. The result is either discovering x or reducing the set size to a fraction of the original.

(b) (5 points) Give the recurrence equation of mysearch algorithm.

(c) (5 points) Give the best-case and worst-case runtime complexity of mysearch algorithm? Explain your answer

(d) (5 points) What is the average-case runtime complexity of mysearch algorithm? Explain your answer. Show transcribed image text // return index i such that A[i] = x, return -1 if x is not found int mysearch(A, left, right, x) { //add statements }

Solutions

Expert Solution

(a) Following is the implementation in C++

#include<iostream>
using namespace std;

// return index i such that A[i] = x
// return -1 if x is not found
int mysearch(int A[], int left, int right, int x)
{
        // Base case
        // The search space has been exhausted
        // return -1
        if(left > right)
                return -1;

        // Getting the index of n/4 element
        int mid1 = left + (right - left) / 4;

        // Getting the index of 3n/4 element
        int mid2 = right - (right - left) / 4;

        // If the element is equal to the n/4th element
        // return it's index
        if (A[mid1] == x)
                return mid1;

        // If the element is equal to the (3n/4)th element
        // return it's index
        if (A[mid2] == x)
                return mid2;

        // If the element is less than the (n/4)th element
        // recur to the first quarter of the array
        if (x < A[mid1])
                return mysearch(A, left, mid1 - 1, x);

        // If the element is greater than the (3n/4)th element
        // recur to the fourth quarter of the array
        else if (x > A[mid2])
                return mysearch(A, mid2 + 1, right, x);

        // Recur to the middle portion of the array
        else
                return mysearch(A, mid1 + 1, mid2 - 1, x);
}

// Driver code
int main()
{
        int n = 5;
        int A[n] = {1, 2, 3, 4, 5};
        cout << mysearch(A, 0, n - 1, 1);
        return 0; 
}

(b) The recurrence relation

(c)

The best case of this algorithm is when the first comparison/guess is correct. It means, regardless of the size of the array, we'll always get the result in constant time. So the best case complexity is O(1).

The worst case of this algorithm is when the middle element has to be found. It means, every time the search space would reduce to half it's size

Solving the above recurrence, we get,

(d)

The average case of this algorithm can be computed by taking the average of the number of iterations required if an element is at any position belonging to [0, 1, ........, n-1] respectively

To be more clear,

As evident from the tree structure above,

Number of nodes at level i = 3i

At every level, 2 matches can be done (at index n/4 and index 3n/4)


Related Solutions

public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return...
public int getIndexOfWord(String[] arr, String word){ // if found, return the index of word, otherwise return -1 } public boolean areArrays(int[] arr1, int[] arr2){ // 1. initial check: both arrays need to have the same length, return false if not the same // 2. return true if both given arrats are equals(same values in the same indices), false otherwise } public boolean areArraysEqual(String[] arr1, String[] arr2){ // 1. initial check: both arrays need to have the same length, return false...
Find the correlation between the country (Philippines) stock index return and the USA index return. Would...
Find the correlation between the country (Philippines) stock index return and the USA index return. Would it be beneficial from the point of view of international portfolio diversification to invest in this country for an investor holding only the USA index portfolio?
With the following, what is the rate of return of period 1, if the index value is weighted?
An Index has the following stocks:StockPricePriceSharesSharesPQQ20252020HIP50251020LOP15132525With the following, what is the rate of return of period 1, if the index value is weighted?
Accounting Rate of Return Payback Period Net Present Value Internal Rate of Return Profitability Index 1)...
Accounting Rate of Return Payback Period Net Present Value Internal Rate of Return Profitability Index 1) Select three of the analytical tools and provide supportive statements explaining how each can be used to screen and/or rank future available projects. 2) Select one of the analytical tools listed and provide supportive statements explaining why you believe it provides the most important information in the decision process.
1. Consider the following realized annual returns: Year End Index Realized Return Stock A Realized Return...
1. Consider the following realized annual returns: Year End Index Realized Return Stock A Realized Return 2006 23.6% 46.3% 2007 24.7% 26.7% 2008 30.5% 86.9% 2009 9.0% 23.1% 2010 -2.0% 0.2% 2011 -17.3% -3.2% 2012 -24.3% -27.0% 2013 32.2% 27.9% 2014 4.4% -5.1% 2015 7.4% -11.3% The average annual return on Stock A from 2006 to 2015 is closest to: 18.2% 16.40% 18.7% 29.9% 2. Use the table for the question(s) below. Consider the following average annual returns: Investment Average...
An index consists of the following securities. What is the value-weighted index return? What is the...
An index consists of the following securities. What is the value-weighted index return? What is the percentage value-weighted index return? (Round your answer to 2 decimal places. Do not round intermediate steps. Omit the "%" sign in your response.) Price per Share Stock Shares Outstanding Beginning of Year End of Year H 3,000 $15 $23 I 7,500 13 10 J 3,000 33 35
C code required /* * isGreater - if x > y then return 1, else return...
C code required /* * isGreater - if x > y then return 1, else return 0 * Example: isGreater(4,5) = 0, isGreater(5,4) = 1 * Legal ops: ! ~ & ^ | + << >> * Max ops: 24 * Rating: 3 */
Consider the following C code: long fact(long x) { if (x <= 1) { return 1;...
Consider the following C code: long fact(long x) { if (x <= 1) { return 1; } long px = x; long fx = fact(x - 1); return px * fx; } Copy the above C code into a C file. For each line of C code responsible for a push or pop operation (either directly or as a result of a call or return), add a comment describing the stack operation and its purpose. Hint: try compiling the C...
1. NPV, IRR, Profitability Index You are reviewing a new project. The required return for assets...
1. NPV, IRR, Profitability Index You are reviewing a new project. The required return for assets of this risk level is 12%. The estimated cash flows are: ◦      Year 0: CF = −165,000 ◦      Year 1: CF = 63,120; ◦      Year 2: CF = 70,800; ◦      Year 3: CF = 91,080; }  What is the NPV, IRR and the profitability index? }  Should you accept or reject this project? 2. Payback, Discounted Payback and AAR You are reviewing a new project. The required return for assets...
1(i) Show, if (X, d) is a metric space, then d∗ : X × X →...
1(i) Show, if (X, d) is a metric space, then d∗ : X × X → [0,∞) defined by d∗(x, y) = d(x, y) /1 + d(x, y) is a metric on X. Feel free to use the fact: if a, b are nonnegative real numbers and a ≤ b, then a/1+a ≤ b/1+b . 1(ii) Suppose A ⊂ B ⊂ R. Show, if A is not empty and B is bounded below, then both inf(A) and inf(B) exist and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT