Question

Design and analysis of algorithms

Problem 5. Given a sorted array of distinct integers A[1- -n], you want to find out whether there is an index I for which Ai-

Type in answer

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

Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

  • Function func(a,lo,high)
    • len = hi - lo + 1
    • mid = len / 2 + lo
    • if(len==1)
      • if(a[mid]==mid)
        • RETURN mid;
      • Else
        • RETURN -1
      • EndIf
    • Else
      • if(a[mid]>mid)
        • RETURN func(a, lo, mid - 1);
      • Else
        • RETURN func(a, mid, hi);
      • EndIf
    • EndIf
  • EndFunction
The above algo works in O(logn) since it is the modification of binary search

Kindly revert for any queries

Thanks.

Add a comment
Answer #2

Algo(A[1,.......,n-1)

1. if n==1, return (A[n]==n)

2. m=(n/2)

3. if A[m] =m , return (true)

4. else if A[m]>m, return (Algo(A[1,.....,m-1]))

5. else return (Algo(A[m+1,....,n]))


Each division reduce the problem size by half, and we do a constant

amount of work in each function call.  This gives us the following re- currence relation:-

T(n) =T(n/2) +O(1)  

which solves to   T(n) =O(log n).


Add a comment
Know the answer?
Add Answer to:
Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct...
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
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