Question

Divide and Conquer & Algorithm Design 5. (20 points) Consider the following algorithm Precondition: S is a sorted list index

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

Explanation of the Above code:

In the above algorithm, we are searching a number.

Actually it is a Binary search algorithm

-------------------------------------------------------------------------------------------------------------------------------------------

let's we have the following array:

array = [4, 5,7, 17 , 20] --Sorted Array

Here is the following parameter passed :

index low --> index 0 of the array

index high --> last index of the array i.e. index 4

S[] --> this is the array

number x --> number we are searching let's suppose 17

---------------------------------------------------------------------------------------------------------------------------------

Now , we check if index low is less than high, we calculate mid point

mid = (low + high ) / 2

mid = (0 + 4 ) / 2 = 2

mid = 2

S[mid] = 7

Now , we check if S[mid] == x, we get the result

Otherwise, if x is less than S[mid], then we pass the array from start the one less to the mid

else, if x is greater than S[mid[, then we pass the array from mid+1 to the end.

Just we do it recursively, untill we get the number x in array or the index low becomes greater than high

  

Add a comment
Know the answer?
Add Answer to:
Divide and Conquer & Algorithm Design 5. (20 points) Consider the following algorithm Precondition: S is...
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