Question

You are given an array A of integers in sorted order. However, you do not know the length n of the array. Assume that in our
0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a):

Since the given array is sorted, we can make use of binarySearch

Algorithm of binarySearch:

int binarySearch(A[],key):

  • start=0
  • end=A.length-1
  • while(start<end):
    • int middle=(start+end)/2
    • if(A[middle]==key)
      • return middle
    • if(A[middle]>key)
      • start=middle+1
    • else
      • end=middle-1
  • return INT_MAX

And time complexity of above code is O(logn) where n is the length of array

(b):

If we observe the code,

initially in step-1, size of search array is n.

In step-2, size of search array is n/2

In step-3, size of search array is n/4

In step-4, size of search array is n/8 and continues till jey value is found or size of array becomes 1 or less.

So number of steps possible are n + n/2 + n/4 + n/8 + ..... + 1 which is logarithm sequence whose value is O(logn). And at the same time in each step we perform operations of time of O(1).

So the final time complexity is O(logn)*O(1) = O(logn)

Mention in comments if any mistakes or errors are found. Thank you.

Add a comment
Know the answer?
Add Answer to:
You are given an array A of integers in sorted order. However, you do not know...
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