GATE CSE
First time here? Checkout the FAQ!
x
+1 vote
300 views
If the binary search algorithm determines that the scanned argument is in upper half of the array,which of the following statement will set the appropriate variable to the appropriate value.

A) startsub= middlesub-1;

B) startsub=middlesub+1;

C)stopsub=middlesub-1;

D)stopsub=middlesub+1;
asked in DS by (461 points)   | 300 views

2 Answers

+3 votes
Best answer
Option B is Ans

Cz as we know the Searching element is in Upper half then We will Increase the middlesub index  by 1. So that our search space is reduced by half.

So stopsub as it is and startsub=middlesub+1

So option B is correct Ans.
answered by Veteran (18.6k points)  
selected by
For the binary search ,the array is order(ascending).So if we know searching element is in upper half ,means that we should continue search in upper half.So our lower index would change and not the upperindex..

My confusion is with the understanding of the question.what the question mean when it says "Scanned argument".Is it the key that we want to search (search argument) or the scanned array element.

If search argument is less than the middle element the we do stopsub=middle -1;

And if the search argument is greater than middle element then we do startsub=middlesub+1;

Please solve my doubt around the question understanding.
Oh..Yes I have drawn the array vertically ;;So I Thought that Upper half means LHS..

But Now I drawn it horizontally..Cz max people do so.

&Ur Doubt:--It is the search element

 

See my Edited Ans..->>Thanks for Comment
+1 vote
B. startsub = middlesub + 1
answered by Loyal (3.7k points)  


Top Users Sep 2017
  1. Habibkhan

    6970 Points

  2. Warrior

    2490 Points

  3. Arjun

    2368 Points

  4. rishu_darkshadow

    2136 Points

  5. A_i_$_h

    2004 Points

  6. nikunj

    1980 Points

  7. makhdoom ghaya

    1760 Points

  8. manu00x

    1756 Points

  9. Bikram

    1744 Points

  10. SiddharthMahapatra

    1718 Points


26,060 questions
33,669 answers
79,747 comments
31,080 users